25

I am setting:

map.setMyLocationEnabled(true)

But I am wondering what this really does. What I know:

  1. I get a locate me button in the upper right corner of the map
  2. I get a blue icon on the map that represents my current location

Here is my concern. I am writing a location aware app but I am concerned about battery. Elsewhere in my app (via preferences) I set up a LocationManager and listen for location events so I can log them. But part of my preferences is the accuracy at which I get updates and the interval.

When I turn off my LocationManager:

locationManange.removeUpdates(LocationListener listener);

I expect to turn off location services for the entire app. Yet I still see the little GPS icon in my phones header bar indicating the app is getting location updates. I know that is coming from the fact that I set this on my map view:

map.setMyLocationEnabled(true);

I originally thought that if you did not listen for Location updates by setting up a LocationManager that set map.setMyLocationEnabled(true) would have no effect (ie no my location icon, or button). However that is not true.

Does that mean that by calling map.setMyLocationEnabled(true) google is setting up its own LocationManager with its own settings? I would really like to call map.setMyLocationEnabled(true), but have it use my settings for location updates not whatever google is doing under the hood. Is that possible? Did I miss something in the docs? I cannot find anything about the accuracy or interval that is setup on a LocationManager when I call map.setMyLocationEnabled(true).

lostintranslation
  • 23,756
  • 50
  • 159
  • 262
  • After your call to `removeUpdates()`, are you sure that GPS updates are not due to other apps? (e.g. Google Maps itself, or anything else). Have you tried this while your app is uninstalled? – xav Mar 19 '14 at 22:56
  • Yes I am sure. If I remove the one line of code 'map.setMyLocationEnabled(true);' The GPS are off. – lostintranslation Mar 19 '14 at 23:06

1 Answers1

14

Does that mean that by calling map.setMyLocationEnabled(true) google is setting up its own LocationManager with its own settings?

Not exactly. Maps V2 uses LocationClient by default.

I would really like to call map.setMyLocationEnabled(true), but have it use my settings for location updates not whatever google is doing under the hood. Is that possible?

You can use setLocationSource() to supply your own location data for use with the my-location layer. Here is a sample project demonstrating this.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Your example also sets up the LocationManager. Can the LocationManager be setup elsewhere in the code? In my map code I implemented everything you have but the location manager, yet I never get location updates. Basically I have other code that implements LocationListener and sets up a LocationManager since I get location updates outside of the map view as well. – lostintranslation Mar 20 '14 at 00:17
  • I think I know what I need to do. It should work. I will post my code tomorrow in case anyone else has a similar issue. Thanks @CommonsWare – lostintranslation Mar 20 '14 at 00:35
  • @lostintranslation: "Can the LocationManager be setup elsewhere in the code?" -- in principle, sure. I just didn't have any other code. :-) So long as you can get the locations over to your `GoogleMap` when needed, where in your code you have the `LocationListener` really does not matter. You might use an event bus, for example (e.g., `LocalBroadcastManager`, Square's Otto, greenrobot's EventBus). – CommonsWare Mar 20 '14 at 00:46
  • @CommonsWare, as we all know that Map.setMyLocationEnabled(true); will enable a button which directly lead us to current location on button click, but i want to know that is there a way that i don't need to click a button but it will directly animate camera to current location? i know that without even clicking on button still shows us current location but it wont animate camera to that location bu zooming up. there is one deprecated method getmyLocation but it gives null data. – Avi Patel Oct 21 '18 at 17:25
  • 1
    @AviPatel: Use `LocationManager` or Play Services' fused location provider to get the device's location. Then, use methods on `GoogleMap` to move the "camera" to point the map at that location. – CommonsWare Oct 21 '18 at 18:00
  • @CommonsWare, I know that way but i thought if it would be possible to do it without using those(by those i mean locationmanager,fusedlocationproviderclient) and if i can zoom directly in like google maps does. I am currently using same fusedlocationproviderrclient, but if it's possible to do that without using it. – Avi Patel Oct 21 '18 at 19:06
  • 1
    @AviPatel: AFAIK, Google wants you to use the fused location provider, which is why they deprecated and discontinued the `getMyLocation()` option. – CommonsWare Oct 21 '18 at 19:13
  • @CommonsWare, Thanks for the info and i am reading your book on android architecture thanks for that as well. you are contributing well to the community. appretiate your help. – Avi Patel Oct 21 '18 at 19:41