Google Maps Android api v2 and current location
Solution 1
You can call startPerc.remove();
to delete only this marker.
From here
Solution 2
Why not just use startPerc.setPosition
to move the marker instead of keep adding and removing...
Solution 3
set the position of the marker when you get another location by this method
marker. set position (location)
Solution 4
you should use googlemap.clear() before adding the marker because everytime when location changed it add new marker on map with oldest marker. so use googlemap.clear() before add the marker in location changed. it will remove oldest marker and add new marker. googlemap is the instance of GoogleMap Class.
Related videos on Youtube
antedesk
I'm a Computer Engineer and a Dr in Applied Electronic. I attended Roma Tre University where I completed my PhD at the Engineering Department. During my PhD, I worked for the Signal Processing for Telecommunications and Economics (SP4TE) lab and I had the chance to collaborate with international laboratories, such as CERIAS of Purdue University and the Department of Electronics and Comm. Eng. of Tampere University of Technology. My research activity has focused on different fields, so that I could apply my knowledge as a computer engineer in both telecommunications and IT. In particular, I worked in the fields of Cognitive Radio Technology (CRT), Android-based app design and development, design and development for radio devices, Natural Language Processing & Sentiment Analysis, and Computer Vision. My passion for IT has led me to learn mobile programming and develop native mobile applications for Android and Windows Phone OS as an independent developer and freelancer for several companies. I'm also collaborating as an author with HTML.it, one of the most authoritative Italian websites on IT, to redact guides and articles about mobile programming. I'm currently working in the R&D team of Immobiliare.it SpA to develop novel tools for data processing and analyses for Immobiliare.it's products and our international partners.
Updated on July 09, 2022Comments
-
antedesk almost 2 years
I'm trying to use the new google maps android api v2 and i'm developing an app for android 2.3.3 or higher. It's a very simple app: it takes the user current location (using gps or networks signal) it gets from db a POI using direction api, it drives the user to the POI.
My problem is get the user current location. thanks to this post How to get the current location in Google Maps Android API v2? I learned that i can't update current position using new google api. Another problem is that I can set my position using
GoogleMap setMyLocationEnabled(boolean enabled)
but I can't usegetMyLocation()
to know where user is.I used this guide http://www.vogella.com/articles/AndroidLocationAPI/article.html#maps_mylocation to get my location, and i tried to integrate inside my Activity to draw user position.
here my code:
Manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="it.mappe" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="it.mappe.permission.MAPS_RECEIVE" /> <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="16" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="MYGMAPSKEY" /> <activity android:name="it.mappe.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
main Activity
package it.mappe; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import android.content.Context; import android.content.Intent; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.provider.Settings; import android.support.v4.app.FragmentActivity; import android.widget.Toast; public class MainActivity extends FragmentActivity implements LocationListener { private GoogleMap map; private static final LatLng ROMA = new LatLng(42.093230818037,11.7971813678741); private LocationManager locationManager; private String provider; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); boolean enabledGPS = service .isProviderEnabled(LocationManager.GPS_PROVIDER); boolean enabledWiFi = service .isProviderEnabled(LocationManager.NETWORK_PROVIDER); // Check if enabled and if not send user to the GSP settings // Better solution would be to display a dialog and suggesting to // go to the settings if (!enabledGPS) { Toast.makeText(this, "GPS signal not found", Toast.LENGTH_LONG).show(); Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); } locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // Define the criteria how to select the locatioin provider -> use // default Criteria criteria = new Criteria(); provider = locationManager.getBestProvider(criteria, false); Location location = locationManager.getLastKnownLocation(provider); // Initialize the location fields if (location != null) { Toast.makeText(this, "Selected Provider " + provider, Toast.LENGTH_SHORT).show(); onLocationChanged(location); } else { //do something } } /* Request updates at startup */ @Override protected void onResume() { super.onResume(); locationManager.requestLocationUpdates(provider, 400, 1, this); } /* Remove the locationlistener updates when Activity is paused */ @Override protected void onPause() { super.onPause(); locationManager.removeUpdates(this); } @Override public void onLocationChanged(Location location) { double lat = location.getLatitude(); double lng = location.getLongitude(); Toast.makeText(this, "Location " + lat+","+lng, Toast.LENGTH_LONG).show(); LatLng coordinate = new LatLng(lat, lng); Toast.makeText(this, "Location " + coordinate.latitude+","+coordinate.longitude, Toast.LENGTH_LONG).show(); Marker startPerc = map.addMarker(new MarkerOptions() .position(coordinate) .title("Start") .snippet("Inizio del percorso") .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))); } @Override public void onProviderDisabled(String provider) { Toast.makeText(this, "Enabled new provider " + provider, Toast.LENGTH_SHORT).show(); } @Override public void onProviderEnabled(String provider) { Toast.makeText(this, "Disabled provider " + provider, Toast.LENGTH_SHORT).show(); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } }
main layout
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment" />
It works but not so good, as you can see in the pictures eveytime there is a new location it adds a new Marker (ex Overlay I think... I never use old google maps api).
How can I show only one markers?
In addition on my map i need to display also POI's marker, so i think that it's not a good solution delate all marker and redraw it on the maps. There is another best way to get user current location, updating it every moment displaying just one custom marker?!
-
antedesk over 11 yearsI'm trying it, I think i can add a private Marker currentLocation and set it to current user location, in this way i can delete it when i have a new location and add the new marker.
-
antedesk over 11 yearsit works. thank you. Just a quickly question: are you able to explain me why locationMenager decide to get always network priveder instead of gps provide?
-
Adrián Rodríguez over 11 yearsHave you tried with this Criteria?
Criteria criteria = Criteria() { Accuracy = Accuracy.Fine };
-
antedesk over 11 yearsno, now i try it... but i think that another problem is that I decide the provider inside onCreate(..) method. now I'm reading also this developer.android.com/training/basics/location/… wich explain how to handle multiple sources of location updates