Reputation: 2679
FYI: I've searched for an appropriate answer but couldn't find one. Most of them are old and used deprecated methods.
What I need is, when the app start it will display google map with marker at the current position. I can set marker using latitude and longitude. But, I couldn't find any way of getting current lat-long using GPS.
Could you please help me to get the current lat-long using GPS?
Thanks in advance.
Upvotes: 0
Views: 7140
Reputation: 783
Check out my answer on a similar question here: https://stackoverflow.com/a/52209682/9375838
Use this: https://github.com/delight-im/Android-SimpleLocation
If your location is enabled, you need just 3 lines of code:
SimpleLocation location = new SimpleLocation(this); //or context instead of this
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Upvotes: 0
Reputation: 2679
Here is my code that I am using to get my current Lat-Lng value:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
double lat = location.getLatitude();
double lng = location.getLongitude();
Hope this will help you.
Upvotes: 2
Reputation: 1221
You can use GPS Tracker that is available on github. Functions getLongitude()
and getLatitude()
will return the current location.
Upvotes: -1