gpt4 book ai didi

android - 如何使用 fragment 类在 mapview 上获取当前位置?

转载 作者:搜寻专家 更新时间:2023-11-01 08:33:40 25 4
gpt4 key购买 nike

你好我是 android 开发的新手,我想使用 fragment 类在 mapview 中获取当前位置。当我添加 setMyLocationEnabled 方法时,它正在请求权限,并且我已经在 list 中添加了所有权限。请帮助我。

Gmaps.java( fragment )

public class Gmaps extends Fragment implements OnMapReadyCallback {

private GoogleMap googleMap;
private MapView mapView;
private boolean mapsSupported = true;
private GoogleApiClient mGoogleApiClient;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MapsInitializer.initialize(getActivity());

if (mapView != null) {
mapView.onCreate(savedInstanceState);
}
initializeMap();
}

private void initializeMap() {
if (googleMap == null && mapsSupported) {
mapView = (MapView) getActivity().findViewById(R.id.map);
googleMap = mapView.getMap();

double latitude = 0.00;
double longitude = 0.00;

MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Marker");

googleMap.addMarker(marker);

CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(0, 0)).zoom(12).build();

googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));


googleMap.getUiSettings().setZoomControlsEnabled(true); // true to enable
googleMap.getUiSettings().setZoomGesturesEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.getUiSettings().setRotateGesturesEnabled(true);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final FrameLayout p = (FrameLayout) inflater.inflate(R.layout.fragment_gmaps, container, false);
mapView = (MapView) p.findViewById(R.id.map);

return p;
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

@Override
public void onResume() {
super.onResume();
mapView.onResume();
initializeMap();
}

@Override
public void onPause() {
super.onPause();
mapView.onPause();
}

@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

@Override
public void onMapReady(GoogleMap googleMap) {

}

在 list 中,我添加了所有这些用于谷歌地图服务的权限

最佳答案

根据 the documentation :

If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request.

这就是为什么即使您已经在 list 文件中声明了权限,您仍然需要在运行时请求它们的原因。

作为解决方法,您可以设置 minSdkVersion < 23,但也如文档所述:

Note: Beginning with Android 6.0 (API level 23), users can revoke permissions from any app at any time, even if the app targets a lower API level. You should test your app to verify that it behaves properly when it's missing a needed permission, regardless of what API level your app targets.

此外,根据 Permissions Best Practices您应该针对这两种权限模型进行测试,以提供更好的用户体验。

关于android - 如何使用 fragment 类在 mapview 上获取当前位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38473246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com