The LocationManager
API on Android seems like it's a bit of a pain to use for an application that only needs an occasional and rough approximation of the user's location.
The app I'm working on isn't really a location app per se, but it does need to get the user's location in order to display a list of nearby businesses. It doesn't need to worry about if the user is moving around or anything like that.
Here's what I'd like to do:
- Show the user a list of nearby locations.
- Preload the user's location so that by the time I need it in
Activity
X, it will be available. - I don't particularly care about accuracy or frequency of update. Just grabbing one location is sufficient as long as it's not way off. Maybe if I want to be fancy I'll update the location once every few mins or so, but it's not a huge priority.
- Work for any device as long as it has either a GPS or a Network Location provider.
It seems like it shouldn't be that hard, but it appears to me that I have to spin up two different location providers (GPS and NETWORK) and manage each's lifecycle. Not only that, but I have to duplicate the same code in multiple activities to satisfy #2. I've tried using getBestProvider()
in the past to cut the solution down to just using one location provider, but that seems to only give you the best "theoretical" provider rather than the provider that's actually going to give you the best results.
Is there a simpler way to accomplish this?
I have created small application with step by step description to gets current locations GPS co-ordinates.
Complete example source code in below URL:
See How it works :
All we need to do is add this permission in manifest file
and create LocationManager instance like this
Check GPS is enabled or not
then implement LocationListener and Get Coordinates
here is the sample code to do
After searching for best implementation how to get best precise user location I managed to combine all the best methods and come up with following class:
This class tries to connect to
min_gps_sat_count
satellites if GPS is enabled. Else returnsLocationManager.getBestProvider()
location. Check the code!This is the code that provides user current location
create Maps Activty:
main.xml:
and define following permission in manifest:
With Fedor's solution I've experienced multiple execution of the callback
gotLocation
. It seems to be due to a race condition in the overriddenLocationListener.onLocationChanged
method, when gotLocation method is 'long enough'. I'm not sure, but I guessremoveUpdates
prevents the enqueueing of new messages in the Looper queue, but it doesn't remove those already enqueued but not yet consumed. Hence the race condition.To reduce the probability of this wrong behavior it's possible to call removeUpdates before firing the onLocationChanged event, but still we have the race condition.
The best solution I found is to replace
requestLocationUpdates
withrequestSingleUpdate
.This is my version, based on Fedor's solution, using an Handler to send a message to the looper thread:
I use this class from a customized looper thread, like the following one:
where Coordinates is a simple class with two properties: latitude and longitude.
You could always just use LocationManager.getLastKnownLocation() but like it says it could be out of date.
And a simple way to get a general location could be registering for the network (usually pretty fast).
and then doing
in the
onLocationChanged()
method of the listener.After seeing all the answers, and question (Simplest and Robust). I got clicked about only library Android-ReactiveLocation.
When i made an location tracking app. Then i realized that its very typical to handle location tracking with optimised with battery.
So i want tell freshers and also developers who don't want to maintain their location code with future optimisations. Use this library.
Dependencies to put in app level build.gradle
Pros to use this lib: