我正在将Mapbox Api与Android配合使用,并且尝试获取用户的当前位置,但是只有在关闭应用程序并重新打开时才能到达当前位置。 我正在使用类似的依赖项:
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'
如果您能帮助我理解问题所在,我将不胜感激。
@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);
}
}