So I've tried many different solutions as found online and on StackOverflow but I just can't seem to get this to work. Here is what I am working with right now which I have from a similar post on SO. What am I doing wrong?
public void getLocationName() {
String strAdd = "";
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(49.123124, -122.652404, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
strAdd = strReturnedAddress.toString();
Log.w("My Current loction address", "" + strReturnedAddress.toString());
} else {
Log.w("My Current loction address", "No Address returned!");
}
} catch (Exception e) {
e.printStackTrace();
Log.w("My Current loction address", "Cannot get Address!");
}
Toast.makeText(getApplicationContext(), "Distance between two locations is: " + strAdd.toString(), Toast.LENGTH_LONG).show();
}
It always throws the exception, so is it my coordinates?
This is a working code I have, try it.
public void GiveResult() {
Geocoder myLocation = new Geocoder(getApplicationContext(),
Locale.getDefault());
try {
List<Address> myList = myLocation.getFromLocation(latitude,
longitude, 1);
if (myList != null && myList.size() > 0) {
Address address = myList.get(0);
result = address.getLocality();
}
Iterator<Address> it = myList.iterator();
while (it.hasNext()) {
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText("Data is" + it.next());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try this,
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getAddressLine(0);
getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.