我是 Java 和 Android Studios 的新手。我目前正在尝试将 MapBox 地图放入片段中,并获取我的设备位置。它有点工作,地图也在片段中启动,我的位置也被发现了,但是一旦我按下设备上的返回按钮,地图就会崩溃,应用程序虽然没有关闭但没有响应。我收到的 logcat 消息是“2020-05-18 12:02:46.392 28026-28125/com.example.projektas5 W/libEGL: EGLNativeWindowType 0x7d2c6f4010”。此外,我只能通过允许电话设置中的权限来获取我的设备位置,因为 permissionsManager.requestLocationPermissions(this) 仅适用于活动。如果有人可以帮助我,将不胜感激。我的代码如下:
public class SecondPage extends Fragment implements OnMapReadyCallback, LocationEngineListener, PermissionsListener {
View root;
MapView mapView;
private MapboxMap map;
private PermissionsManager permissionsManager;
private LocationEngine locationEngine;
private LocationLayerPlugin locationLayerPlugin;
private Location originLocation;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Mapbox.getInstance(getContext(),getString(R.string.access_token));
root = inflater.inflate(R.layout.second_page, container, false);
mapView =(MapView) (root).findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
return root;
}
@Override
public void onMapReady(MapboxMap mapboxMap) {
map = mapboxMap;
enableLocation();
}
private void enableLocation() {
if (PermissionsManager.areLocationPermissionsGranted(getContext())) {
initializeLocationEngine();
initializeLocationLayer();
} else {
permissionsManager = new PermissionsManager(this);
// permissionsManager.requestLocationPermissions(this);
}
}
private void initializeLocationEngine() {
locationEngine = new LocationEngineProvider(getContext()).obtainBestLocationEngineAvailable();
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.activate();
Location lastLocation = locationEngine.getLastLocation();
if (lastLocation != null) {
originLocation = lastLocation;
setCameraPosition(lastLocation);
} else {
locationEngine.addLocationEngineListener(this);
}
}
private void initializeLocationLayer() {
locationLayerPlugin = new LocationLayerPlugin(mapView, map, locationEngine);
locationLayerPlugin.setLocationLayerEnabled(true);
locationLayerPlugin.setCameraMode(CameraMode.TRACKING);
locationLayerPlugin.setRenderMode(RenderMode.NORMAL);
}
private void setCameraPosition(Location location) {
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),
location.getLongitude()), 13.0));
}
@Override
@SuppressWarnings("MissingPermission")
public void onConnected() {
locationEngine.requestLocationUpdates();
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
originLocation = location;
setCameraPosition(location);
}
}
@Override
public void onExplanationNeeded(List<String> permissionsToExplain) {
//Present toast or dialog. Need to do this on my own
}
@Override
public void onPermissionResult(boolean granted) {
if (granted) {
enableLocation();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
permissionsManager.onRequestPermissionsResult(requestCode, permissions,grantResults);
}
@Override
public void onStart() {
super.onStart();
if (locationEngine != null) {
locationEngine.removeLocationUpdates();
}
if (locationLayerPlugin != null) {
locationLayerPlugin.onStart();
}
mapView.onStart();
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onStop() {
super.onStop();
if (locationEngine != null) {
locationEngine.removeLocationUpdates();
}
if (locationLayerPlugin != null) {
locationLayerPlugin.onStop();
}
mapView.onStop();
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onDestroy() {
super.onDestroy();
if (locationEngine != null) {
locationEngine.deactivate();
}
mapView.onDestroy();
}
}