map.setMyLocationEnabled(true); is not working

耗尽温柔 提交于 2019-12-24 07:23:08

问题


I have just started working on a android project and is using the Google Map API.

I am able to fetch the map on my app but when i try to enable the current location with map.setMyLocationEnabled(true);, the app says unfortunately app has stopped working.

When I removed the line map.setMyLocationEnabled(true); then it worked fine. Can anyone please help me to get the current location button enabled.


回答1:


If you are using android 6.0 or above, you should:

  1. Make sure you added ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION in your AndroidManifest file.
  2. You need to check permission at run-time in this 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;

If you want an easy-to-use library for permission checking, I suggest Permission Dispatcher.




回答2:


According to this, you need to have requested permission for either ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.

My guess is you haven't and your app is throwing a SecurityException.



来源:https://stackoverflow.com/questions/39395739/map-setmylocationenabledtrue-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!