0

i am trying to get the user current city (not the exact address, only the city)

for getting the country , i am using this code:

  String locale = context.getResources().getConfiguration().locale.getCountry(); 

i tried this code for getting the city:

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());

however it return for me an empty string: ""

p.s: i already have a ACCESS_FINE_LOCATION and internet permission in the manifest file

thanks alot

Adir Rahamim
  • 453
  • 1
  • 12
  • 31
  • http://stackoverflow.com/questions/2296377/how-to-get-city-name-from-latitude-and-longitude-values-in-google-map-android – maszter Mar 22 '13 at 22:57
  • i didnt understand from where do i need to get the lat and lng variables, if i am not connected to gps? (only wifi) thanks alot – Adir Rahamim Mar 22 '13 at 23:04

1 Answers1

0

First of all the way you are getting the country just gives you the country which has been defined in the settings. So this might be correct in many cases, but it won't work for users who have set an other country in their settings then they actually are right now.

Geocoder does not give a result for any latitude, longitude combination, one of the reasons might be lack of networkconnection or you are missing the base service which is used by the API.

You should also make sure that Geocoder.isPresent() returns true - if not, it won't work for any locations. Geocoder's isPresent() method always returns false

Community
  • 1
  • 1
fish
  • 828
  • 1
  • 6
  • 14
  • thanks for answer, also ,i didnt understand from where do i need to get the lat and lng variables, if i am not connected to gps? (only wifi) thanks alot – Adir Rahamim Mar 22 '13 at 23:06
  • Just use the [LocationManager](http://developer.android.com/reference/android/location/LocationManager.html) - there is no difference for you as a developer if you determine GPS or a network based position. Android does all the nescessary stuff for you. As a developer you can just force the system to use GPS for instance. [Localtion Strategies](http://developer.android.com/guide/topics/location /strategies.html) – fish Mar 22 '13 at 23:23
  • i understand how can i get my location with gps, however not with wifi, can you give me an example please? thanks alot – Adir Rahamim Mar 22 '13 at 23:35