Как я показываю свое местоположение в Android с помощью Mapbox
Я хочу показать свое местоположение в Android, я пытался, но я не понимаю, почему он не показывает мое местоположение, но здесь не отображается мой код, так что кто-нибудь поможет мне.
private MapView mapView;
private MapboxMap map;
LocationEngine locationEngine;
LocationLayerPlugin locationLayerPlugin;
PermissionsManager permissionsManager;
Location originLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, "my key");
setContentView(R.layout.activity_mabs_map_box);
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(MapboxMap mapboxMap) {
/* LocationPluginActivity.this.map = map;
enableLocationPlugin();*/
map = mapboxMap;
locationEnable();
mapboxMap.getUiSettings().setZoomControlsEnabled(true);
mapboxMap.getUiSettings().setZoomGesturesEnabled(true);
mapboxMap.getUiSettings().setScrollGesturesEnabled(true);
mapboxMap.getUiSettings().setAllGesturesEnabled(true);
}
void locationEnable() {
if (PermissionsManager.areLocationPermissionsGranted(this)) {
intialLocationEngine();
intializLocationLayer();
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}
void intialLocationEngine() {
locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.activate();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location lastLocation = locationEngine.getLastLocation();
if (lastLocation != null) {
originLayout = lastLocation;
setCamerpostion(lastLocation);
} else {
locationEngine.addLocationEngineListener(this);
}
}
void intializLocationLayer() {
locationLayerPlugin = new LocationLayerPlugin(mapView, map, locationEngine);
locationLayerPlugin.setLocationLayerEnabled(true);
locationLayerPlugin.setCameraMode(CameraMode.TRACKING);
locationLayerPlugin.setRenderMode(RenderMode.NORMAL);
}
void setCamerpostion(Location camerpostion) {
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(camerpostion.getLatitude(), camerpostion.getLongitude()), 13.0));
}
@Override
public void onConnected() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationEngine.requestLocationUpdates();
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
originLayout = location;
setCamerpostion(location);
}
}
@Override
public void onExplanationNeeded(List<String> permissionsToExplain) {
}
@Override
public void onPermissionResult(boolean granted) {
if (granted) {
locationEnable();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@SuppressWarnings("MissingPermission")
@Override
public void onStart() {
super.onStart();
if (locationEngine != null)
locationEngine.requestLocationUpdates();
mapView.onStart();
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (locationEngine!=null)
{
locationEngine.deactivate();
} mapView.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
и это показывает мне ошибку этого
java.lang.NullPointerException: попытка вызвать виртуальный метод 'boolean android.os.Bundle.getBoolean(java.lang.String, boolean)' для пустой ссылки на объект в com.mapbox.mapboxsdk.storage.FileSource.getCachePath(FileSource. Java:88) в com.mapbox.mapboxsdk.storage.FileSource.getResourcesCachePath(FileSource.java:188) в com.mapbox.mapboxsdk.storage.FileSource.getInstance(FileSource.java:66) в com.mapbox.mapboxsdk.maps.MapView.onStart(MapView.java:365) в com.example.mapsconfig.MabsMapBox.onStart(MabsMapBox.java:148) в android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1249) в android.app.Activity. executeStart(Activity.java:6886) в android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2684) в android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782) в android.app.ActivityThread.-wrap12(ActivityThread. Java) в android.app.ActivityThread$H.handleMessage(ActivityThread.java:1521) в android.os.Handler.dispatchMessage(Handler.java:102) на android.os.Looper.loop(Looper.java:163) на android.app.ActivityThread.main(ActivityThread.java:6228) на java.lang.reflect.Method.invoke(собственный метод) на com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) в com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)