Get address using Geocoder in android
I tried to get the address of a particular location by giving static geocordinates. I was not able to fetch the address. Can someone please help. I just need to check whether this function works for me.
Here is my snippet.
Geocoder geocoder = new Geocoder(AddressSimulator.this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(1.352566007, 103.78921587, 1);
System.out.println("Addresses size"+addresses.size());
Address size is obtained as zero. I tried with few other geocordinates also, but address size is always returned as 0. Experts开发者_如何转开发, kindly help me resolve this.
Looking forward for your valuable help/suggestions,
Best Regards, Rony
Try the one from google or from rpc geocoder. For google maps you have to get a key from google.
for geocoder they respond for only one request in every 15 sec from the same ip.
string geocoderUri = String.Format("http://maps.google.com/maps/geo?q={0},{1},{2}&output=csv"
+ "&key= < Your Key > ", street, city, state);
or
string geocoderUri = string.Format("http://rpc.geocoder.us/service/rest?address={0},{1},{2}", street, city, state);
Try this one. It will provide you the street, area, country name. Also see this doc; here you can find more methods.
http://developer.android.com/reference/android/location/Address.html
try {
Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
if (addresses.isEmpty()) {
addres.setText("Waiting for Location");
}
else {
if (addresses.size() > 0) {
addres.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
//Toast.makeText(getApplicationContext(), "Address:- " + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show();
}
}
}
catch (Exception e) {
e.printStackTrace(); // getFromLocation() may sometimes fail
}
Hope this will help....
Make sure you have the INTERNET security permissions set in your AndroidManifest.xml. You need the INTERNET permission to do the lookup. I know you're doing a static lat,long lookup but if you want to do get lat and long from GPS, you'll also need to add the ACCESS_FINE_LOCATION permission to enable the GPS.
精彩评论