Animate initialisation of MyLocationTracking.TRACKING_FOLLOW
Platform: Android Mapbox SDK version: 4.2.0-beta.3
This is a feature request. Can we enhance the location tracking API to support animating the camera?
Currently, when I do...
trackingSettings.setMyLocationTrackingMode(MyLocationTracking.TRACKING_FOLLOW);
... the camera will jump directly to my current location. However, it would be nice if we could have the camera animate to my current location.
Note, the animation should work smoothly even when padding is set with MyLocationViewSettings.setPadding(...).
@ahuang13 thank you for reaching out and requesting this feature. iOS is currently exposing this feature also they allow to listen to a callback that is invoked when the initial animation has stopped. This would allow for more complex user interaction and animations.
In the past I have done this by first getting the user location, and animating the camera to that position. You can then either use the OnFinish callback of the camera animation or use a Handler to begin tracking the user location once the animation is finished.
@cammace, when I use the onFinish() callback like in the following snippet, I get an infinite loop.
mMapboxMap.animateCamera(cameraUpdate, new MapboxMap.CancelableCallback() {
@Override
public void onCancel() {
}
@Override
public void onFinish() {
mMapboxMap.getTrackingSettings().setMyLocationTrackingMode(MyLocationTracking.TRACKING_FOLLOW);
}
});
It looks like it's because MapView.flyTo() actually saves the callback (inside another callback) and only removes the callback when mapChange == REGION_DID_CHANGE_ANIMATED, which evaluates to false under certain situations (I don't know when) causing the callback to not be removed and therefore repeatedly called in an infinite loop.
void flyTo(double bearing, LatLng center, long duration, double pitch, double zoom, @Nullable final MapboxMap.CancelableCallback cancelableCallback) {
if (destroyed) {
return;
}
nativeMapView.cancelTransitions();
// Register callbacks early enough
if (cancelableCallback != null) {
addOnMapChangedListener(new OnMapChangedListener() {
@Override
public void onMapChanged(@MapChange int change) {
if (change == REGION_DID_CHANGE_ANIMATED) {
cancelableCallback.onFinish();
// Clean up after self
removeOnMapChangedListener(this);
}
}
});
}
nativeMapView.flyTo(bearing, center, duration, pitch, zoom);
}
Can you advise?
@ahuang13 this shouldn't be the case as it wasn't in the original PR https://github.com/mapbox/mapbox-gl-native/pull/6891/files. I'm guessing this was not correctly rebased. Fixing up now.
update: it seems that the code was correctly rebased, trying to reproduce.
I'm not able to reproduce the use-case shown above. This is how it works for me on master:

Though I was able to find an similar issue, where executing another animateTo from an onfinish callback that was executed with animateTo just cancels the next cameraupdate resulting in not changing the camera. For this I'm currently thinking to simplify the code (no more 2 callbacks) and look into posting every camera update to the message queue, this will make sure that the callbacks is set to null and state is handled correclty when we execute something from onFiinish. This will also fix from #6769.
update: one thing that comes to mind is if the changed animation isn't large enough, this could possibly not trigger a region change animated. Going to play around with this API and see if we can make it work.
@ahuang13 which version of the SDK are you using? are you building from source, if yes which branch? You can follow the changes I proposed above in #6919.
Thanks, @tobrun. I've tried with both 4.2.0-beta.3 and 4.2.0-beta.4.
LocationLayer is going to replace MyLocationView. Feel free to recreate a ticket in that repository.