How can I get my location in latitude and longitude from android code. I dont want to use GPS as it drains battery and dont wana give my city or country name. Can you please share the code sample to get my current location.
Asked
Active
Viewed 633 times
2 Answers
1
I dont want to use GPS as it drains battery
Emm, it really does not take that much of battery if you are using a single shot fix. The battery drains out only in the tracking mode.
Like Chris said, use the Network Provider.
Another option is to get your location based on the IP address :
- http://www.hostip.info/ : API here
- Other web api's to get the location from the ip address
- and some more
-
Thanks for your response but I can't use IP address method :( – Jewel Nov 25 '11 at 12:09
0
If your application has the coarse location permission and the user has turned on "use network based location", you can get approximate location from the network provider:
LocationManager locMgr = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
Location loc = locMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
It is possible that getLastKnownLocation will return null. In that case, you'll need to request location updates and use a LocationListener to capture the next location.

Chris
- 22,923
- 4
- 56
- 50
-
For more info about Location Listener, http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/ – Nov 25 '11 at 11:33