首页 > 解决方案 > Android 中的 MapBox:无法使用 requestLocationPermissions 方法在片段中请求位置权限

问题描述

对于我的编码学校项目,我在片段中使用 mapbox 地图。我设法通过找到我的(用户)位置将地图添加到片段中。但是,只有当我手动进入手机设置并允许应用程序的位置权限时,它才会找到我的位置。我一直在关注 mapbox 教程和文档,但似乎大部分内容都是为要在活动中使用的 MapBox 地图而编写的。在 mapbox 教程中,我看到正在使用以下内容:

private void enableLocation() {
    if (PermissionsManager.areLocationPermissionsGranted(getContext())) {
        initializeLocationEngine();
        initializeLocationLayer();
    } else {
        permissionsManager = new PermissionsManager(this); // THIS, HOWEVER WORKS IN THE FRAGMENT
       permissionsManager.requestLocationPermissions(this); //HERE IS MY PROBLEM
    }
}

上面在我的应用程序中显示的代码我在片段 onCreateView 中使用,并且由于 requestLocationPermissions 方法的参数是活动,我无法使该方法运行。我尝试了不同的选项来引用片段关联的 mainActivity,但是没有成功。因此,我的问题是 - 是否有一些简单的快速解决这个问题的方法,我可以通过这种方法以某种方式传递活动?

标签: javaandroidandroid-fragmentsmapbox

解决方案


您应该能够requestPermissions(permissionsList, REQUEST_CODE);从片段中请求许可。
无需传递mainActivity上下文。

你如何启用你的位置?我通常喜欢这样做:

@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {

    mapboxMap.setStyle(new Style.Builder()
            .fromUri("mapbox://styles/mapbox/navigation-guidance-night-v4")
            .withSource(new GeoJsonSource(ISOCHRONE_RESPONSE_GEOJSON_SOURCE_ID)), style -> {

        mapboxMap.getUiSettings().setCompassMargins(0, 180, 20, 0);
        // Map is set up and the style has loaded. Now you can add
        // data or make other map adjustments
        MainActivity.this.mapboxMap = mapboxMap;

        trafficPlugin = new TrafficPlugin(mapView, mapboxMap, Objects.requireNonNull(
                mapboxMap.getStyle()));
        this.trafficPlugin.setVisibility(false);

        this.style = style;

        // Create an empty GeoJSON source using the empty feature collection
        setUpSource();

        mapboxMap.addOnMapClickListener(MainActivity.this);
        mapboxMap.addOnMapLongClickListener(MainActivity.this);

        initEmptyLayer(Objects.requireNonNull(mapboxMap.getStyle()));
        setUpImage(Objects.requireNonNull(mapboxMap.getStyle()));

        enableLocationComponent(style);
    });
}

然后在我的 enableLocationCompnent 函数上:

@SuppressWarnings({"MissingPermission"})
private void enableLocationComponent(Style style) {
    // Check if permissions are enabled and if not request
    if (PermissionsManager.areLocationPermissionsGranted(this)) {

        LocationComponentOptions locationComponentOptions = LocationComponentOptions.builder(this)
                .layerAbove("empty-layer")
                .build();

        LocationComponentActivationOptions locationComponentActivationOptions = LocationComponentActivationOptions
                .builder(this, style)
                .locationComponentOptions(locationComponentOptions)
                .build();

        locationComponent = mapboxMap.getLocationComponent();
        // Activate with options
        locationComponent.activateLocationComponent(locationComponentActivationOptions);

        // Enable to make component visible
        locationComponent.setLocationComponentEnabled(true);

        // Set the component's camera mode
        locationComponent.setCameraMode(CameraMode.TRACKING);
        locationComponent.setRenderMode(RenderMode.COMPASS);
        initLocationEngine();

    } else {
        permissionsManager = new PermissionsManager(this);
        permissionsManager.requestLocationPermissions(this);
    }
}

希望有帮助!


推荐阅读