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?
Here's what I do:
Here's how I use my class:
And here's MyLocation class:
Somebody may also want to modify my logic. For example if you get update from Network provider don't stop listeners but continue waiting. GPS gives more accurate data so it's worth waiting for it. If timer elapses and you've got update from Network but not from GPS then you can use value provided from Network.
One more approach is to use LocationClient http://developer.android.com/training/location/retrieve-current.html. But it requires Google Play Services apk to be installed on user device.
This is the way I am requesting User Permissions.
Outside your application tag in AndroidManifest.xml add these permission requests.
Then add Google's Location dependencies in the App Gradle file.
Now declare some Global variables.
In OnCreate method of your Activity (I was not able to Format the Code Properly, Apology for that)
Now create both the functions.
Second Method
For Constant.kt
Before I answer, I just want to be upfront about the fact that I’m affiliated with Neura. Now to your question. So, instead of wasting your time behind coding to get your user’s location, you can use our SDK. It takes less than 30 mins to integrate. Neura provides you with your user’s location/activity,and you can configure your application accordingly to perform certain tasks after you receive the data. Here’s a few reasons to use Neura over other Activity recognition API’s:
It requires lots of things in place to get location updates in android, requires lots of bolierplate code.
You need to take care of
I have created Android-EasyLocation (small android library) which will take care all this stuff and you can focus on business logic.
All you need is extend EasyLocationActivity and this
or
Checkout sample app and steps needed here at https://github.com/akhgupta/Android-EasyLocation
Simple and best way for GeoLocation.
Actualy we can use the two providers both. And they just share a public listener:
This is necessary because the OnLocationChanged() method always need to be called in time.