I am using Mapbox Api with Android and I am trying to get the current position of the user but I can reach the current location only on closing the app and reopening it. I am using like dependencies :
implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:6.8.1'
implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-locationlayer:0.11.0'
implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.26.0'
I will be very grateful if you can help me understanding what's the problem.
@SuppressWarnings({"MissingPermission"})
private void enableLocationComponent() {
locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.activate();
Location lastLocation = locationEngine.getLastLocation();
if(lastLocation != null){
originLocation = lastLocation;
}else{
locationEngine.addLocationEngineListener(this);
}
if (PermissionsManager.areLocationPermissionsGranted(this)) {
// Get an instance of the component
LocationComponent locationComponent = map.getLocationComponent();
// Activate with options
locationComponent.activateLocationComponent(this);
// Enable to make component visible
locationComponent.setLocationComponentEnabled(true);
// Set the component's camera mode
locationComponent.setCameraMode(CameraMode.TRACKING);
// Set the component's render mode
locationComponent.setRenderMode(RenderMode.COMPASS);
originLocation = locationComponent.getLastKnownLocation();
// Log.d(TAG, "enableLocationComponent: " + locationComponent.getLastKnownLocation().getLatitude());
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}