android - map.setMyLocationEnabled(true);不管用

标签 android google-maps android-location

我刚刚开始开发一个 android 项目,并且正在使用 Google Map API。

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

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

最佳答案

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

  1. 确保在 AndroidManifest 文件中添加了 ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION
  2. 您需要在运行时检查此链接的权限: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 .

关于android - map.setMyLocationEnabled(true);不管用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39395739/

相关文章:

android - android中高效的gps服务

java - 将按钮添加到 LinearLayout 不起作用

java - 单击按钮即可执行多个步骤操作

android - 使用 Mockito 和 Dagger 的 SharedPreferences

android - 道路 googleapi : Some places missing and lines passing through edifices

android - 尽管我已按照文档中的步骤操作,但地点选择器(谷歌地图)会立即关闭

android - 地理围栏形状,API v2 android

java - FusedLocationProviderClient.removeLocationUpdates 总是返回失败

java - Android - 在 onCreate 之前更改类变量

android - 用于位置跟踪的前台服务与 WorkManager