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?
The recommended way to do this is to use
LocationClient
:First, define location update interval values. Adjust this to your needs.
Have your
Activity
implementGooglePlayServicesClient.ConnectionCallbacks
,GooglePlayServicesClient.OnConnectionFailedListener
, andLocationListener
.Then, set up a
LocationClient
in theonCreate()
method of yourActivity
:Add the required methods to your
Activity
;onConnected()
is the method that is called when theLocationClient
connects.onLocationChanged()
is where you'll retrieve the most up-to-date location.Be sure to connect/disconnect the
LocationClient
so it's only using extra battery when absolutely necessary and so the GPS doesn't run indefinitely. TheLocationClient
must be connected in order to get data from it.Get the user's location. First try using the
LocationClient
; if that fails, fall back to theLocationManager
.To get and show the user's current location, you could also use
MyLocationOverlay
. Suppose you have amapView
field in your activity. All you would need to do to show the user location is the following:This gets the current location from either the GPS or the network. If both fail,
enableMyLocation()
will returnfalse
.As for the locations of things around the area, an
ItemizedOverlay
should do the trick.I hope I haven't misunderstood your question. Good luck.
Recently refactored to obtain the location of the code, learn some good ideas, and finally achieved a relatively perfect library and Demo.
Complete code: https://github.com/bingerz/FastLocation/blob/master/fastlocationlib/src/main/java/cn/bingerz/fastlocation/FastLocation.java
*Each request to complete the location, it is best to removeUpdates, otherwise the phone status bar will always display the positioning icon.
I have written detailed tutorial covering current location here on demonuts.com.You can find more description here and also you can download whole demo source code for better understanding.
There are already many answers there but I want to show latest way to get location using Google API, so new programmers can use new method:
First of all, put this in gradle file
then implement necessary interfaces
declare instances
put this in
onCreate()
At last, override necessary methods
Don't forget to start GPS in your device before running app.
By using FusedLocationProviderApi which is the latest API and the best among the available possibilities to get location in Android. add this in build.gradle file
you can get full source code by this url http://javapapers.com/android/android-location-fused-provider/