我是Java和Android Studios的新手。我目前正试图把MapBox地图放到一个片段中,并获取我的设备位置。它的工作和地图在一个片段中启动,以及我的位置被发现,但一旦我按下我的设备上的返回按钮,地图崩溃和应用程序虽然没有关闭,但变得无响应。我得到的logcat消息是 "2020-05-18 12:02:46.392 28026-28125com.example.stastaas5 WlibEGL: 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();
}
}
这将是有帮助的使用地图SDK SupportMapFragment
https:/docs.mapbox.comandroidmapsexamplesshow-a-users-location-on-a-fragment。 展示了如何使用Mapbox Maps SDK的 LocationComponent
在...中 SupportMapFragment
.
根据这个例子工作?如果你仍然得到一个崩溃,发布更多的logcat消息。搜索任何有 Mbgl
中或搜索 FATAL
. 该 "2020-05-18 12:02:46.392 28026-28125/com.example.projektas5 W/libEGL: EGLNativeWindowType 0x7d2c6f4010 ".
信息对你按下后退键时找出问题所在没有太大帮助。
langsmith 谢谢你的回答。实际上,我已经找到了地图崩溃的原因,在我的代码末尾,我必须添加onDestroyView(),见下面。
@Override
public void onDestroyView() {
super.onDestroyView();
if (locationEngine != null) {
locationEngine.deactivate();
}
// mapView.onDestroyView();
}