英文:
MapBox in Android : Unable to request location permissions in a fragment using requestLocationPermissions method
问题
在我的编程学校项目中,我正在一个片段中使用 Mapbox 地图。我成功地将地图添加到一个片段中,并找到了我的(用户)位置。然而,只有在我手动进入手机设置并允许应用程序获取位置权限后,它才能找到我的位置。我按照 Mapbox 的教程和文档进行操作,但似乎大部分内容都是针对在活动中使用 Mapbox 地图编写的。在 Mapbox 的教程中,我看到以下代码被使用:
private void enableLocation() {
if (PermissionsManager.areLocationPermissionsGranted(getContext())) {
initializeLocationEngine();
initializeLocationLayer();
} else {
permissionsManager = new PermissionsManager(this); // 但是这在片段中有效
permissionsManager.requestLocationPermissions(this); // 这里是我的问题
}
}
我在我的应用程序中使用上述代码片段中的内容来设置片段的 onCreateView
方法,由于 requestLocationPermissions
方法的参数是活动,我无法运行该方法。我尝试了不同的选项来引用与片段关联的主活动,但都没有成功。因此,我的问题是 - 是否有一些简单的解决办法可以绕过这个问题,在这个方法中可以传递活动吗?
英文:
For my coding school project I am using mapbox map in a fragment. I manage to add map to a fragment with finding my (the user) location. However, it finds my location only when I go manually to my phone settings and allow the location permissions for the app. I was following mapbox tutorials and documentation, however it seems most of it is being written for MapBox map to be used in the activity. In the mapbox tutorials I see that the following is being used:
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
}
}
The code that I have shown above in my app I use in fragments onCreateView, and since the parameter for the requestLocationPermissions method is the activity, I am unable to make that method run. I tried different options to refer to mainActivity that the fragment is associated to, however with no success. Therefore, my question is - is there some simple quick go around this problem where I can pass somehow activity in this method?
答案1
得分: 0
你应该能够在片段内部执行 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);
// 地图设置完成,样式加载完毕。现在你可以添加数据或进行其他地图调整
MainActivity.this.mapboxMap = mapboxMap;
trafficPlugin = new TrafficPlugin(mapView, mapboxMap, Objects.requireNonNull(
mapboxMap.getStyle()));
this.trafficPlugin.setVisibility(false);
this.style = style;
// 使用空要素集创建空的 GeoJSON 源
setUpSource();
mapboxMap.addOnMapClickListener(MainActivity.this);
mapboxMap.addOnMapLongClickListener(MainActivity.this);
initEmptyLayer(Objects.requireNonNull(mapboxMap.getStyle()));
setUpImage(Objects.requireNonNull(mapboxMap.getStyle()));
enableLocationComponent(style);
});
}
然后在我的 enableLocationComponent
函数中:
@SuppressWarnings({"MissingPermission"})
private void enableLocationComponent(Style style) {
// 检查权限是否已启用,如果未启用则请求
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();
// 根据选项激活
locationComponent.activateLocationComponent(locationComponentActivationOptions);
// 启用以使组件可见
locationComponent.setLocationComponentEnabled(true);
// 设置组件的相机模式
locationComponent.setCameraMode(CameraMode.TRACKING);
locationComponent.setRenderMode(RenderMode.COMPASS);
initLocationEngine();
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}
希望对你有所帮助!
英文:
You should be able to do requestPermissions(permissionsList, REQUEST_CODE);
to request permission from within the fragment. <br> There is no need to pass the mainActivity
context.
How do you enable your location? I generally like to do it this way:
@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);
});
}
Then on my enableLocationCompnent function:
@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);
}
}
Hope that helps!
专注分享java语言的经验与见解,让所有开发者获益!
评论