mapbox / mapbox-plugins-android

Mapbox Android Plugins are a collection of libraries that extend our other SDKs, helping you design powerful mapping features while the plugins handle most of the heavy lifting.
https://www.mapbox.com/android-docs/plugins/overview/
BSD 2-Clause "Simplified" License
241 stars 119 forks source link

Animation not working after removing location updates and adding them again #126

Closed kkalera closed 6 years ago

kkalera commented 6 years ago

Hi,

I've been trying to use the location layer plugin but I'm having some issues when I remove the location updates and add them again. When I start the application, the location marker moves smoothly when changing location. However; when I remove the location updates from the locationEngine (in onPause or onStop functions) and I call "requestLocationUpdates" on the locationEngine, the navigation icon just snaps to the new location.

I have provided a gif below to show you: (ignore the reverse part, apparently my gif maker also reversed it) videotogif_2017 10 03_20 22 07 1

Code is copied from the example to be sure I'm not the problem. I have the following code in my onMapReadyCallback: locationEngine = new LostLocationEngine(MainActivity.this); locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY); locationEngine.addLocationEngineListener(MainActivity.this); locationEngine.activate(); locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine); getLifecycle().addObserver(locationLayerPlugin);

In my "onStart()" and "onResume()" lifecycle methods i'm calling the following function: public void enableLocation(){ if(ContextCompat.checkSelfPermission(getApplicationContext(),Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){ if (locationEngine != null) { locationEngine.requestLocationUpdates(); locationEngine.addLocationEngineListener(MainActivity.this); } if(locationLayerPlugin != null && locationLayerPlugin.getLocationLayerMode() != LocationLayerMode.NAVIGATION){ locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.NAVIGATION); } }else{ Log.e("trp", "Location permission not granted"); } }

And finally in my "onPause()" ,"onStop()" and "onDestroy()" lifecycle methods, I'm calling the following function; `public void disableLocation(){

    if (locationEngine != null) {
        locationEngine.removeLocationEngineListener(MainActivity.this);
        locationEngine.removeLocationUpdates();
    }
}`
kkalera commented 6 years ago

I found something weird regarding this issue. When my user is on a route, I'm snapping the location update to that route. When the user's location is snapped , I'm using NavigationLayerPlugin.forceLocationUpdate(Location).

The weird thing is, that when I use that function after snapping the users location to the route, everything works fine. But if I use that function on the location update itself, it's jumpy.

I have included another gif to showcase this; videotogif_2017 10 12_08 50 24

I was thinking it might have something to do with the location received from OnLocationChanged so I already tried creating a new Location object. The same way I do when I'm using the snapping function, this doesn't solve the issue.

kkalera commented 6 years ago

I found a work-around. Only updating the location in the 'OnLocationChanged' function when the last update was more than 500ms ago, solves the problem.

If anybody is interested in the code (beware it's in kotlin. But adapting it to java is pretty easy): private var lastLocUpdate:Long = -1 if(lastLocUpdate.equals(-1) || (System.currentTimeMillis() - lastLocUpdate) > 500){ locationLayerPlugin.forceLocationUpdate(location) lastLocUpdate = System.currentTimeMillis() }

cammace commented 6 years ago

Thanks for posting a workaround. This is a similar issue to https://github.com/mapbox/mapbox-plugins-android/issues/215 and the fix for that issue should also fix this one. Closing and will track the issue in that issue.