0

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.

Reno
  • 33,594
  • 11
  • 89
  • 102
Jewel
  • 31
  • 1
  • 8

2 Answers2

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 :

Community
  • 1
  • 1
Reno
  • 33,594
  • 11
  • 89
  • 102
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