I am using the MyLocationNewOverlay
to show the user's current location. I already managed to place a marker, when the location is found for the first time (e.g. when the runOnFirstFix()
function is called:
/* set start location */
mapController = mapView.getController();
mapController.setZoom(12);
GeoPoint startPoint = new GeoPoint(mucLocation.latitude, mucLocation.longitude);
mapController.setCenter(startPoint);
/* MyLocation overlay */
mLocationOverlay = new MyLocationNewOverlay(mapView);
mLocationOverlay.enableMyLocation();
// mLocationOverlay.enableFollowLocation();
mLocationOverlay.setDrawAccuracyEnabled(true);
mLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
Log.e("Location", "runOnFirstFix");
mapController.animateTo(mLocationOverlay.getMyLocation());
Marker currentLocation = new Marker(mapView);
currentLocation.setIcon(ContextCompat.getDrawable(mContext, R.drawable.ic_location));
currentLocation.setPosition(mLocationOverlay.getMyLocation());
currentLocation.setTitle("Hier bin ich");
mapView.getOverlays().add(currentLocation);
}
});
Now I would like to update the marker's location, whenever a location update is received. As far as I know this is not possible with MyLocationOverlay
, but I hope that it will be possible with the new MyLocationNewOverlay
. In the Documentation I saw that there is the enableMyLocation()
which allows to receive location updates and the enableFollowLocation()
which keeps centering the map at the current location. So it seems that the overlay is already able to provide regular location updates.
I know I could make use of Android's LocationListener
, but it seems that this functionality is already inside MyLocationNewOverlay
since its able to follow the location.
Does anyone know if there is a way to receive location updates through that overlay and could give an example about how to do that? I noticed the MyLocationNewOverlay.onLocationChanged()
function but couldn't figure out how to use it. Any ideas?
Late answer but hope it will help you.
As you noticed, there is MyLocationNewOverlay.onLocationChanged()
method. In order to use it, you need to override it. Example below:
MyLocationNewOverlay locationOverlay = new MyLocationNewOverlay(gpsMyLocationProvider, mapView) {
@Override
public void onLocationChanged(Location location, IMyLocationProvider source) {
super.onLocationChanger(location, source);
// This is where you can use updated location
useNewLocation(location);
}
}
You may also receive location updates directly from gpsMyLocationProvider
. Procedure is exactly the same, just override onLocationChanged()
method, call super
and do whatever you need with your data.