java - 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,但是没有成功。因此,我的问题是 - 是否有一些简单的快速解决这个问题的方法,我可以通过这种方法以某种方式传递活动?
解决方案
您应该能够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);
}
}
希望有帮助!
推荐阅读
- javascript - javascript函数只返回未定义
- c# - 如何在 c# 代码中使用 xamarin 在 BackgroundImageSource 上设置不透明度
- python - 为什么 pygame 精灵在 pygame 菜单中无法正常工作?
- facebook - 新的 Facebook 照片文件名
- c# - 将列表与数据库记录进行比较,如果存在 - 更新,如果新 - 创建(EF Core)
- javascript - 谷歌表格时间戳通过应用程序脚本
- c++ - 使用重载 >> 从文件创建对象正在创建空白对象(C++)
- javascript - 构造函数原型上的方法在调用时抛出 TypeError
- windows - First React Native Windows App - 在 VSCode 中运行调试返回错误
- python - 我需要确定如何绘制搜索词数据中另一个变量(国家)使用的最常用词以及相应的国家