My app is currently using Maps by Google Play Services
speficying:
mMap.setMyLocationEnabled(true);
I realize each time I am displaying the map in my app:
- the location is indicated on the map by a blue dot
- a location icon is displaying in the top bar
- if I go into Settings/Location of the phone, my app is reported as "High battery use"
However, I can see there are apps that use Maps and still show the location blue dot, but the location icon doesn't appear in top bar and their battery usage is low.
My app currently grants both permissions:
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
My question is:
how can I show the location blue dot with low battery usage?
is it possible to specify the accuracy/battery usage by code?
UPDATE
Actually I realized that the way to do it is to use the GoogleApiClient
's FusedLocationApi
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
I have configured the GoogleApiClient inside my Activity, calling:
GoogleApiClient.connect()
on the Activity's startGoogleApiClient.disconnect()
on the Activity's stop
on the onConnected
callback I set the criteria for the location updates: fastest interval of 1 minute with low power priority:
private static final LocationRequest REQUEST = LocationRequest.create()
.setFastestInterval(60000) // in milliseconds
.setInterval(180000) // in milliseconds
.setPriority(LocationRequest.PRIORITY_LOW_POWER);
@Override
public void onConnected(Bundle bundle) {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient,
REQUEST,
this); // LocationListener
}
I have tested that the GoogleApiClient connects correctly at start, but for some reasons whenever I visit the fragment with the embedded MapView, I still get the high battery use for my app on the Settings/Location screen!
It seems the MapView is ignoring these low power criterias!
It's stated here that
So "My Location" dot on map is fed by
FusedLocationProviderApi
. And as you grant permissionandroid.permission.ACCESS_FINE_LOCATION
you allowFusedLocationProviderApi
for your app to get data from GPS which may cause high battery use.So add only
android.permission.ACCESS_COARSE_LOCATION
permission to manifest and Android should not blame you for battery usage.You will want to make your activity (or better a separate object for this purpose) implement the
LocationSource
interface.It is pretty simple you need to store the listener passed in the
activate()
method and call it when the location is updated and forget it whendeactivate()
is called. See this answer for an example, you will probably want to update it to use theFusedLocationProvider
.Once you have this set up you can pass your activity as the
LocationSource
for the map like somMap.setLocationSource(this)
(documentation).This will stop the map from using its default
LocationSource
which uses the high battery use location services.FINALLY FOUND THE SOLUTION!!! thanks to Tristan for his answer!
By default, GoogleMap uses its on location provider, which is not the Fused Location Provider. In order to use the Fused Location Provider (which allows you to control the location accuracy and power consumption) you need to explicitely set the map location source with
GoogleMap.setLocationSource()
(documentation)I am reporting here a sample activity to do that:
You can do so by using Network provider classes You can use below code AppLocationService.java // Special for getting current location with low battery usage (same like Battery saver mode in nexus 5 ,5.0)
Usage of above class MainActivity.java
In this way you can get the current location with GPS mode in high bettery usage mode, after you can set the blue dot or whatever you want
Hope it helps you and all