Android How to - Convert Address to Latitude and Longitude








Question

We would like to know how to convert Address to Latitude and Longitude.

Answer

/*from  w w  w  .j a v  a2  s  .  c  o m*/
import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;

class AddressToCoords {
  String latitude;
  String longitude;

  public AddressToCoords(String address, Context context) {
    Geocoder geocoder = new Geocoder(context);
    List<Address> addresses;
    try {
      addresses = geocoder.getFromLocationName(address + ", Firenze, FI", 1);
    } catch (IOException e) {
      return;
    }
    if (addresses.size() > 0) {
      latitude = Double.valueOf(addresses.get(0).getLatitude()).toString();
      longitude = Double.valueOf(addresses.get(0).getLongitude()).toString();
    }
  }

  public String getLatitude() {
    return latitude;
  }

  public String getLongitude() {
    return longitude;
  }
}