Is there a simple method to jump to current location in Mapbox SDK for Android?

1.8k views Asked by At

I am trying to develop an applicition with the latest Mapbox Maps SDK for Android (v9.5.0).

At the moment, my map can show the location of my device with LocationComponent but there isn't any button in the UI to go back to the device's location and put it in the center, like Google Maps does with the "my location button".

Therefore my question would be: is there any already included method within SDK which I haven't found or I should use a combination of animateCamera() and CameraUpdate() for this purpose?

1

There are 1 answers

1
langsmith On BEST ANSWER

Easiest way would to use animateCamera() and CameraUpdate() with the last known location coordinates. To get the last known coordinates:

if (mapboxMap.getLocationComponent().getLastKnownLocation() != null) {
        Location lastKnownLocation = mapboxMap.getLocationComponent().getLastKnownLocation();
        double lat = lastKnownLocation.getLatitude();
        double longitude = lastKnownLocation.getLongitude();
      }

There are various camera modes as well https://docs.mapbox.com/android/maps/examples/location-component-camera-options. Once the camera is moved, you could change the mode to CameraMode.TRACKING. But this is different than just having the camera move back to the device location without any tracking behavior, as seen in the code snippet above.