I need to get my current location using GPS programmatically. How can i achieve it?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
You need to use latest/newest
Basically what you need to do is:
Then
for the most accurate and reliable location. See my post here:
https://stackoverflow.com/a/33599228/2644905
Do not use LocationListener which is not accurate and has delayed response. To be honest this is easier to implement. Also read documentation: https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient
I will recommend using Smart Location Library
Very simple to use and it wraps the location logic nicely.
For starting the location service:
If you just want to get a single location (not periodic) you can just use the oneFix modifier. Example:
I have created a small application with step by step description to get current location's GPS coordinates.
Complete example source code is in Get Current Location coordinates , City name - in Android.
See how it works:
All we need to do is add this permission in the manifest file:
And create a LocationManager instance like this:
Check if GPS is enabled or not.
And then implement LocationListener and get coordinates:
Here is the sample code to do so
Here is additional information for other answers.
Since Android has
you can register to both and start fetch events from
onLocationChanged(Location location)
from two at the same time. So far so good. Now the question do we need two results or we should take the best. As I knowGPS_PROVIDER
results have better accuracy thanNETWORK_PROVIDER
.Let's define
Location
field:Before we start listen on Location change we will implement the following method. This method returns the last known location, between the GPS and the network one. For this method newer is best.
Each time when we retrieve a new location we will compare it with our previous result.
I add a new method to
onLocationChanged
:Get location of gps by -
Simple and easy way,
Get location using https://github.com/sachinvarma/EasyLocation.
Step 1: Just call
timeInterval -> setInterval(long)(inMilliSeconds) means - set the interval in which you want to get locations.
fastestTimeInterval -> setFastestInterval(long)(inMilliSeconds) means - if a location is available sooner you can get it. (i.e. another app is using the location services).
runAsBackgroundService = True -> (Service will run in Background and updates Frequently(according to the timeInterval and fastestTimeInterval)) runAsBackgroundService = False -> (Service will getDestroyed after a successful location update )
Step 2: Prepare EventBus subscribers: Declare and annotate your subscribing method, optionally specify a thread mode:
eg:
That's all.
Hope it will help someone in future.