map.setMyLocationEnabled(真);不管用

map.setMyLocationEnabled(true); is not working

我刚刚开始从事 android 项目,并且正在使用 Google 地图 API。

我可以在我的应用程序上获取地图,但是当我尝试使用 map.setMyLocationEnabled(true); 启用当前位置时,该应用程序说不幸的是应用程序已停止工作。

当我删除行 map.setMyLocationEnabled(true); 时,它工作正常。谁能帮我启用当前位置按钮。

根据 this,您需要为 ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION 请求许可。

我猜你还没有,你的应用正在抛出 SecurityException

如果您使用的是 android 6.0 或更高版本,您应该:

  1. 确保您在 AndroidManifest 文件中添加了 ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION
  2. 您需要在 运行 时检查权限 link:Requesting Permission

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app's functionality;

如果你想要一个易于使用的库来进行权限检查,我建议 Permission Dispatcher