How to Get Latitude/Longitude from Place Name in Android
Takahiro Iwasa
1 min read
Android GIS Java
It has been more than 3 years since this post was published.
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;
}
}