How to Get Latitude/Longitude from Place Name in Android

How to Get Latitude/Longitude from Place Name in Android

Takahiro Iwasa
(岩佐 孝浩)
Takahiro Iwasa (岩佐 孝浩)
1 min read
Android GIS Java

Latitude/Longitude can be obtained from a place name using android.location.Geocoder in Android. This process is known as Geocoding.

/**
 * Get latitude/longitude from place name.
 * @param context
 * @param Place name
 * @return Array of latitude/longitude, otherwise null
 */
public static double[] getLocationFromPlaceName(Context context, String name) {
    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
    try {
        List<Address> location = geocoder.getFromLocationName(name, 1);
        if (location == null || location.size() < 1) {
            return null;
        }

        Address address = location.get(0);
        double[] latlng = { address.getLatitude(), address.getLongitude() };
        return latlng;
    }
    catch (IOException e) {
        return null;
    }
}
Takahiro Iwasa
(岩佐 孝浩)

Takahiro Iwasa (岩佐 孝浩)

Software Developer at KAKEHASHI Inc.
Involved in the design, development, and operation of the prescription data collection platform