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. Happy Coding!

/**
 * 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 requirements definition, design, and development of cloud-native applications using AWS. Now, building a new prescription data collection platform at KAKEHASHI Inc. Japan AWS Top Engineers 2020-2023.