Reputation: 567
I am making an android application.In my application I want to get the city name of my current location, but I have a problem in my country. If I used Google Geode API it doesn't recognize it, eg: when I try this query which must return information about my location using lat and longitude; it doesn't work.
this is the query : http://maps.googleapis.com/maps/api/geocode/json?latlng=32.3117,35.0272&sensor=true
also when I use this code, I get the address= null:
Geocoder gCoder = new Geocoder(this);
List<Address> addresses;
try {
addresses = gCoder.getFromLocation(lat, lng, 5);
Log.e("get adderess", addresses + "");
if (addresses != null && addresses.size() > 0) {
city = addresses.get(0).getLocality();
System.out.print(addresses.get(0).getLocality());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
the problem is in my country ONLY :(
any help how to solve it ??
Upvotes: 1
Views: 4745
Reputation: 224
If you are getting null as your city, or any other values
Then try this code, it worked fine for me
Geocoder geocoder = new Geocoder(YourActivity.this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(latitude,longitude, 2);
} catch (IOException e) {
e.printStackTrace();
}
String country = addresses.get(1).getCountryName();
String city = addresses.get(1).getLocality();
You are getting null for the first values
In this code, instead of getting the first values, you will get the second one, so instead of typing addresses.get(0)
you will type addresses.get(1)
Upvotes: 0
Reputation:
have u encoded your URL? one needs to encode and decode the url while sending and receiving by http request
Upvotes: 0
Reputation: 4360
This code works for me fine.It gives you city name and country name on behalf of your latitude and longitude.But on some places it didn't gives you the city name i.e Asia etc(some places).
Geocoder geocoder = new Geocoder(getBaseContext(), Locale.getDefault());
Address address;
List<Address> list;
list = geocoder.getFromLocation(latitude,longitude,1);//33.64600, 72.96115
address = list.get(0);
String cityname = address.getLocality();
String countryname = address.getCountryName();
Log.d("cityname",cityname.toString());
Log.d("countryname",countryname.toString());
Also Please add the following uses-permission node within the AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
Upvotes: 2