问题
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:
- Make sure you added
ACCESS_COARSE_LOCATION
andACCESS_FINE_LOCATION
in yourAndroidManifest
file. - 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