0

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?

arne.z
  • 3,242
  • 3
  • 24
  • 46
  • It should update the icon as long as enableMyLocation is turned on. Are you seeing a different behavior? – spy Jan 07 '17 at 01:51
  • You mean the `onLocationChanged()` should update the icon that marks the current location? That functions sounds like a callback that is called when the location changed, but actually, it expects a location as parameter, so I don't understand what's the point of it. – arne.z Jan 07 '17 at 10:43
  • @spy, okay, I just saw what you meant. Didn't get a GPS signal before. But now I saw that the man icon which shows up at my location. So I guess it's already implemented. Sometimes the person icon changes to a green arrow and then back to the person icon. Do you know why? – arne.z Jan 07 '17 at 17:49
  • 2
    person = stationary, green arrow = in movement, showing the direction of moment – spy Jan 07 '17 at 19:39

2 Answers2

1

In Kotlin it would be like this:

val locationOverlay =
            object : MyLocationNewOverlay(GpsMyLocationProvider(this), binding.map) {
                override fun onLocationChanged(location: Location?, source: IMyLocationProvider?) {
                    super.onLocationChanged(location, source)
                    location?.let {
                        Log.i("MyCurrentSpeed", it.speed.toString())
                    }
                }
            }
locationOverlay.enableMyLocation()
binding.map.overlays.add(locationOverlay)

0

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.

S. Dabrowski
  • 51
  • 1
  • 6