1

我是 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();
    }
}
4

2 回答 2

1

SupportMapFragment 使用 Maps SDK https://docs.mapbox.com/android/maps/examples/show-a-users-location-on-a-fragment/会很有帮助,展示了如何LocationComponentSupportMapFragment.

处理那个例子?如果您仍然遇到崩溃,请发布更多 logcat 消息。搜索其中包含的任何Mbgl内容或搜索FATAL. 当您按下后退按钮时,该"2020-05-18 12:02:46.392 28026-28125/com.example.projektas5 W/libEGL: EGLNativeWindowType 0x7d2c6f4010 ".消息对于找出问题所在不是很有帮助。

于 2020-05-18T16:12:40.817 回答
0

langsmith 感谢您的回答。实际上,我设法找到了地图崩溃的原因,在我的代码末尾我必须添加 onDestroyView(),见下文。

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (locationEngine != null) {
            locationEngine.deactivate();
        }
//        mapView.onDestroyView();
    }
于 2020-05-19T10:04:44.240 回答