Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps API Android v2 - "ACCESS_FINE_LOCATION" permission required with my-location layer enabled

I want to show the location of the user on a Google Maps enabling my-location layer, but this functionality requires the "ACCESS_FINE_LOCATION" permission to get the location from the GPS. Is it possible to prevent the GoogleMap from trying to get the location from the GPS?

If I remove the permission "ACCESS_FINE_LOCATION" in the Manifest file, as soon as I try to display the map, the app crashes with the following error:

E/AndroidRuntime(28578): FATAL EXCEPTION: main
E/AndroidRuntime(28578): java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.
E/AndroidRuntime(28578):    at android.os.Parcel.readException(Parcel.java:1425)
E/AndroidRuntime(28578):    at android.os.Parcel.readException(Parcel.java:1379)
E/AndroidRuntime(28578):    at bbj.a(SourceFile:424)
E/AndroidRuntime(28578):    at bbn.a(SourceFile:232)
E/AndroidRuntime(28578):    at maps.al.b.j(Unknown Source)
E/AndroidRuntime(28578):    at uh.h(SourceFile:642)
E/AndroidRuntime(28578):    at un.a(SourceFile:399)
E/AndroidRuntime(28578):    at uj.a(SourceFile:158)
E/AndroidRuntime(28578):    at ui.handleMessage(SourceFile:111)
E/AndroidRuntime(28578):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(28578):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(28578):    at android.app.ActivityThread.main(ActivityThread.java:5041)
E/AndroidRuntime(28578):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(28578):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(28578):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(28578):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(28578):    at dalvik.system.NativeStart.main(Native Method)
like image 417
Donkey Avatar asked Jun 13 '13 14:06

Donkey


People also ask

How do I fix Google Maps does not have permission to use my location?

From the settings menu scroll down to the Accounts section and tap on Google. In the next menu tap on Location settings. On the final location access page swipe the Off button to On. This will allow all of Google's Android apps installed on your phone to access your GPS location.

What is the purpose of setting the user permission for Access_fine_location?

Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION . The permission you choose determines the accuracy of the location returned by the API.


2 Answers

You can use the LocationSource pattern to supply your own locations, which could come from whatever. LocationSource is an interface, which you would implement on some object that can supply location data. You register this with setLocationSource() on the GoogleMap. You implement activate() and deactivate() methods on your LocationSource that will get called to let you know when you should start and stop pushing over locations. As you get location fixes in, you call onLocationChanged() on a supplied OnLocationChangedListener.

This sample project demonstrates the use of LocationSource.

like image 156
CommonsWare Avatar answered Oct 02 '22 14:10

CommonsWare


Is it possible to prevent the GoogleMap from trying to get the location from the GPS?

Default implmentation of LocationSource doesn't necessarily have to use GPS to return accurate locations.

If I remove the permission "ACCESS_FINE_LOCATION" in the Manifest file, as soon as I try to display the map, the app crashes

This is because the default implementation uses LocationClient with LocationRequest.PRIORITY_HIGH_ACCURACY.

For now you can do nothing more than following CommonsWare's answer, but I also encourage you to create a feature request on gmaps-api-issues, so the default implementation doesn't fail when doing GoogleMap.setMyLocationEnabled when you request only ACCESS_COARSE_LOCATION permission, but switches to LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY.

like image 28
MaciejGórski Avatar answered Oct 02 '22 14:10

MaciejGórski