is FusedLocationProviderApi.getLastLocation is HIGH_ACCURACY? - android

I want to ask if getLastLocation() from FusedLocationProviderApi get Location with HIGH_ACCURACCY or not?
when I read the documentation, it says:
public abstract Location getLastLocation (GoogleApiClient client)
Returns the best most recent location currently available.
If a location is not available, which should happen very rarely, null will be returned. The best accuracy available while respecting the location permissions will be returned.
This method provides a simplified way to get location. It is particularly well suited for applications that do not require an accurate location and that do not want to maintain extra logic for location updates.
does the best means get the most accurate location? or like automatically request last location using LocationRequest.PRIORITY_HIGH_ACCURACY?

getLastLocation will not create a new request to get the current location. It gives already available/saved location based on previous location request.
This already available/saved location details is based on the last location data requested by any client/app on the user's device.
Eg: If user has Google Maps installed and running , it gives you the accurate location of the device. In this case once you start your app and check for last location it will give you location with high accuracy.
In case your device doesn't have Google Maps but another app which requested location updates with low accuracy , your getLastLocation will return that low accuracy location .

Related

How getLastLocation API works internally? It returns null even if location setting is ON

I've read some questions about this, but I didn't quite find an answer that I needed. I want to understand how getLastLocation works internally. How last known location cached value is updated?
We have requirement where we need to get last known location of the device. Can anyone explain how getLastLocation works? I have seen that getLastLocation API returns null even if location-setting is ON. At the same time if I checked Google Map app and it is able to get the location (of-course it must be using requestLocationUpdates or any other API).
On my Nexus-5, I observed that getLastLocation returns null even after I launch Google Map app and then launch my app again. Even if location is detected correctly in Google Map app, getLastLocation API returns null.
How Android system updates the last known location? Does it update only when some app requests for location via requestLocationUpdates API?
If Android system updates the last known location automatically then why do getLastLocation API returns null?
getLastLocation API
Getting last known location
Points to be noted:
Location permission is granted.
Location Setting is ON
Same code works on few devices, but fails on few devices.
I am trying to get location from Google's FusedLocationProviderAPI and Android Framework LocationManager API.
I have checked all existing questions on Stackoverflow
It would be great if anyone can explain under what circumstances getLastLocation API can return null even if location setting is ON. Does anyone know how this works internally?
getLastLocation : This method will give you the last location available on your device.
In some cases if your device don't have any last location then it will return null.
You should use onLocationChanged method for current location.
Please see example here you will get it
https://developer.android.com/training/location/receive-location-updates.html
According to doc
public abstract Location getLastLocation (GoogleApiClient client)
Returns the best most recent location currently available.
If a location is not available, which should happen very rarely, null
will be returned. The best accuracy available while respecting the
location permissions will be returned.
This method provides a simplified way to get location. It is
particularly well suited for applications that do not require an
accurate location and that do not want to maintain extra logic for
location updates.
Hope these help you.

Getting user location with minimal impact for battery on Android

I'm starting to develop an app that will stay in background forever and detect when a user is staying in a certain location for a while (and then display a notification to invite the user to open the app to obtain informations about that place).
It's something very similar to what Google Maps does when you're in a restaurant and it shows you a notification to check ratings about it.
What I want is to have a minimal impact on device, so location updates should be very "passive", getting the location only when user is not moving and, if possible, recycling location data that is already got by other activities - maybe by Google Maps itself or other location apps that are running on the devices.
This is not a navigation app, so I don't need to have the live fine location but simply the approximate place with the best accuracy and minimal effort, and only if user is not moving.
LocationListener and onLocationChanged seems to be my men, but can I specify that I don't want to trigger device's sensors and only re-use location data when it's available for other scopes? My app should check these informations and then decide to do a reverse geocode if and when they are accurate enough.
Yes, LocationListener and onLocationChanged are your men, though for a passive implementation, there are a few options you can go through.
First you can check for the last known location, maybe compare it in terms of its time; i.e. getTime() and verify whether it is of use to you.
In terms of code, literally...
Google samples, android location has what is relevant for the last location part:
/**
* Runs when a GoogleApiClient object successfully connects.
*/
#Override
public void onConnected(Bundle connectionHint) {
// Provides a simple way of getting a device's location and is well suited for
// applications that do not require a fine-grained location and that do not need location
// updates. Gets the best and most recent location currently available, which may be null
// in rare cases when a location is not available.
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
Further, you can combine it with LocationRequest object, this helps your implementation as you can call it right after trying getLastLocation() and basically have a more reliable arrangement for obtaining location.
// Create the location request
mLocationRequest = LocationRequest.create()
//priority object needs to be set, the following will definitely get results for you
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
//interval updates can be on the lines of 15mins or 30... acc to your requirement
.setInterval(UPDATE_INTERVAL)
.setFastestInterval(FASTEST_INTERVAL);
// Request location updates
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, this);
i suggest give PRIORITY_NO_POWER a try, could work well in combination with getLastLocation(). These power modes have been added specifically for optimising battery/power consumption and efficiency for retrieving location.

android Google maps : getLastLocation vs requestLocationUpdates

I just need to understand this clearly, getLastLocation() method will give me the last known location and requestLocationUpdates will give me the current location every period of time.
now, i am developing a simple app to track mobile phones, which will be in cars.
my Question are :
1- how exactly getLastLocation() works, will this last known location be updated when the location change
2- which is better, using getLastLocation() to have the initial location then update the current location by using the method onLocationChanged or using requestLocationUpdates to have an up to date location every n sec ?
getLastLocation gives you the last location which was there in LocationClient, it can be null sometimes, say if you don't have any other application installed which uses location services, that's a rare case as most phones have Google maps or some other location dependent apps installed.
getLastLocation is only helpful, when you want location for once and your done with it, but for location updates you should use requestLocationUpdates, and using a service, your application will always get notified, when the location changes.
I hope you are using the FusedLocation Provider from play services, so you can specify how fast location updates you need, to check cases of battery drain and performance.
Also to note here when you use requestLocationUpdates then only you will get any location change updates.
so according to android docs getLastLocation() :-
Returns the best most recent location currently available. If a
location is not available, which should happen very rarely, null will
be returned. The best accuracy available while respecting the location
permissions will be returned. This method provides a simplified way to
get location. It is particularly well suited for applications that do
not require an accurate location and that do not want to maintain
extra logic for location updates.
This is the problem with getLastLocation(),that it is not accurate and it may return null if it does not have any last known location in cache.I have faced this issue many times in my application.So for getting the accurate location you should use requestLocationUpdates() but define the interval according to your need at which time interval you want the updates as a very short time span may cause more power usage.

Receiving current location on Android

I've been working on an app that needs to be location-aware, and I've noticed that there are two (or more) methods of receiving location: with Google Play services (as seen here developer.android.com/training/location/retrieve-current.html#GetLocation) and with Location Manager, Providers etc. (as seen here http://www.vogella.com/tutorials/AndroidLocationAPI/article.html#locationapi_criteria).
What is the difference between these methods (if there is any)? Which one is more accurate?
edit: ok, I see that I sent the wrong link on the first thing. Won't this code (http://developer.android.com/training/location/receive-location-updates.html) give me location updates? Generally, what's the most accurate way to get my location?
The one with the GPS is accurate and that which is based on Network is not. Google Play Service use FUSE api to get the GPS location first, if the location is found (that's great), otherwise it will try to get location fix from Network Tower. In Short the one with GPS is accurate
The first method provides the details of LastKnownLocation. ie. the last location received from GPS or network provider when you or other apps accessed the location services. After that there are chances you moved a lot and it need not be your current location. So if You are planning to create an application that requires accurate location tracing, You should fetch the location as in the "Vogella" method. If the current location is unavailable, you can try using the last known location (As a plan B :-)).

Want to get current location of user in android activity

I need current location of user in my application on based on that location I will search on database!
I am getting current location right now by using example code given but it is not accurate. If no GPS I get location by network provider but it shows sometime 2 location names at the same location. If it is showing one name 1 min before after 2 min it will change to another location.
This code is look but do not know how to use it. Please help me out in this regard!
What is the simplest and most robust way to get the user's current location on Android?
I need to get the location name.
Network location is not as reliable as GPS location, so you might get some fluctuation. That's even more true if you're relying on wifi APs.
You may try to average locations or wait to see if it stabilises in some location in particular, or show the most common location (i.e., the one reported most of the time).

Categories

Resources