4

I need to get the user current city name. If getting the city name with AGPS or anything is good. I need the method or code for getting the current city in android. Please share if you know.

arnp
  • 3,178
  • 6
  • 26
  • 43
  • possible duplicate of [How to get city name from latitude and longitude coordinates in Google Maps?](http://stackoverflow.com/questions/2296377/how-to-get-city-name-from-latitude-and-longitude-coordinates-in-google-maps) – tkanzakic Oct 15 '13 at 12:53

2 Answers2

22

This one is for getting location name from current lat,long

Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0) 
    System.out.println(addresses.get(0).getLocality());
user370305
  • 108,599
  • 23
  • 164
  • 151
  • I tried with you code its not working. i have added internet permission and access fine location permission. do i need to add any more permissions. – arnp Nov 14 '11 at 09:32
  • 1
    try{ Geocoder gcd = new Geocoder(context, Locale.getDefault()); List
    addresses = gcd.getFromLocation(16.045813,76.992188, 1); if (addresses.size() > 0) strlocation = String.valueOf(addresses.get(0).getLocality()); Toast.makeText(this, strlocation, Toast.LENGTH_SHORT).show(); }catch(Exception e) { e.printStackTrace(); }
    – arnp Nov 14 '11 at 09:41
  • java.lang.ClassNotFoundException: com.andro.loc.LocfinderActivity in loader dalvik.system.PathClassLoader[/data/app/com.andro.loc-2.apk] – arnp Nov 14 '11 at 10:04
  • Its your activity class not found in DVM. its not a geocoder exception, Have you mentioned activity name in manifest file. if yes then remove application from emulator and re-install it and if not then just add it in manifest. – user370305 Nov 14 '11 at 10:09
  • I am getting error which says "Timed out waiting for response from server". I have added Internet permission. Do it need something else? – Bhaumik Belani Apr 26 '16 at 17:34
0

I've upvoted user370305 answer but i'll just post an alternative just in case.

You can also find the City name from your IP Address.

Check this site and this answer here

Community
  • 1
  • 1
st0le
  • 33,375
  • 8
  • 89
  • 89