Geocoder

public final class Geocoder
extends Object

java.lang.Object
   ↳ android.location.Geocoder


A class for handling geocoding and reverse geocoding. Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address. The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code.

Use the isPresent() method to determine whether a Geocoder implementation exists on the current device. If no implementation is present, any attempt to geocode will result in an error.

Geocoder implementations are only required to make a best effort to return results in the chosen locale. Note that geocoder implementations may return results in other locales if they have no information available for the chosen locale.

Warning: Geocoding services may provide no guarantees on availability or accuracy. Results are a best guess, and are not guaranteed to be meaningful or correct. Do not use this API for any safety-critical or regulatory compliance purpose.

Summary

Nested classes

interface Geocoder.GeocodeListener

A listener for asynchronous geocoding results. 

Public constructors

Geocoder(Context context)

Constructs a Geocoder localized for Locale#getDefault().

Geocoder(Context context, Locale locale)

Constructs a Geocoder localized for the given locale.

Public methods

List<Address> getFromLocation(double latitude, double longitude, int maxResults)

This method was deprecated in API level 33. Use getFromLocation(double, double, int, android.location.Geocoder.GeocodeListener) instead to avoid blocking a thread waiting for results.

void getFromLocation(double latitude, double longitude, int maxResults, Geocoder.GeocodeListener listener)

Provides an array of Addresses that attempt to describe the area immediately surrounding the given latitude and longitude.

List<Address> getFromLocationName(String locationName, int maxResults, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude)

This method was deprecated in API level 33. Use getFromLocationName(java.lang.String, int, double, double, double, double, android.location.Geocoder.GeocodeListener) instead to avoid blocking a thread waiting for results.

void getFromLocationName(String locationName, int maxResults, double lowerLeftLatitude, double lowerLeftLongitude, double upperRightLatitude, double upperRightLongitude, Geocoder.GeocodeListener listener)

Returns an array of Addresses that attempt to describe the named location, which may be a place name such as "Dalvik, Iceland", an address such as "1600 Amphitheatre Parkway, Mountain View, CA", an airport code such as "SFO", and so forth.

void getFromLocationName(String locationName, int maxResults, Geocoder.GeocodeListener listener)

Provides an array of Addresses that attempt to describe the named location, which may be a place name such as "Dalvik, Iceland", an address such as "1600 Amphitheatre Parkway, Mountain View, CA", an airport code such as "SFO", and so forth.

List<Address> getFromLocationName(String locationName, int maxResults)

This method was deprecated in API level 33. Use getFromLocationName(java.lang.String, int, android.location.Geocoder.GeocodeListener) instead to avoid blocking a thread waiting for results.

static boolean isPresent()

Returns true if there is a geocoder implementation present on the device that may return results.

Inherited methods

Public constructors

Geocoder

Added in API level 1
public Geocoder (Context context)

Constructs a Geocoder localized for Locale#getDefault().

Parameters
context Context: This value cannot be null.

Geocoder

Added in API level 1
public Geocoder (Context context, 
                Locale locale)

Constructs a Geocoder localized for the given locale. Note that geocoder implementations will only make a best effort to return results in the given locale, and there is no guarantee that returned results will be in the specific locale.

Parameters
context Context: This value cannot be null.

locale Locale: This value cannot be null.

Public methods

getFromLocation

Added in API level 1
Deprecated in API level 33
public List<Address> getFromLocation (double latitude, 
                double longitude, 
                int maxResults)

This method was deprecated in API level 33.
Use getFromLocation(double, double, int, android.location.Geocoder.GeocodeListener) instead to avoid blocking a thread waiting for results.

Returns an array of Addresses that attempt to describe the area immediately surrounding the given latitude and longitude. The returned addresses should be localized for the locale provided to this class's constructor.

Warning: Geocoding services may provide no guarantees on availability or accuracy. Results are a best guess, and are not guaranteed to be meaningful or correct. Do NOT use this API for any safety-critical or regulatory compliance purposes.

Warning: This API may hit the network, and may block for excessive amounts of time. It's strongly encouraged to use the asynchronous version of this API. If that is not possible, this should be run on a background thread to avoid blocking other operations.

Parameters
latitude double: the latitude a point for the search Value is between -90D and 90D inclusive

longitude double: the longitude a point for the search Value is between -180D and 180D inclusive

maxResults int: max number of addresses to return. Smaller numbers (1 to 5) are recommended Value is 1 or greater

Returns
List<Address> a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.

Throws
IllegalArgumentException if latitude or longitude is invalid
IOException if there is a failure

getFromLocation

Added in API level 33
public void getFromLocation (double latitude, 
                double longitude, 
                int maxResults, 
                Geocoder.GeocodeListener listener)

Provides an array of Addresses that attempt to describe the area immediately surrounding the given latitude and longitude. The returned addresses should be localized for the locale provided to this class's constructor.

Warning: Geocoding services may provide no guarantees on availability or accuracy. Results are a best guess, and are not guaranteed to be meaningful or correct. Do NOT use this API for any safety-critical or regulatory compliance purposes.

Parameters
latitude double: the latitude a point for the search Value is between -90D and 90D inclusive

longitude double: the longitude a point for the search Value is between -180D and 180D inclusive

maxResults int: max number of addresses to return. Smaller numbers (1 to 5) are recommended Value is 1 or greater

listener Geocoder.GeocodeListener: a listener for receiving results This value cannot be null.

Throws
IllegalArgumentException if latitude or longitude is invalid

getFromLocationName

Added in API level 1
Deprecated in API level 33
public List<Address> getFromLocationName (String locationName, 
                int maxResults, 
                double lowerLeftLatitude, 
                double lowerLeftLongitude, 
                double upperRightLatitude, 
                double upperRightLongitude)

This method was deprecated in API level 33.
Use getFromLocationName(java.lang.String, int, double, double, double, double, android.location.Geocoder.GeocodeListener) instead to avoid blocking a thread waiting for results.

Returns an array of Addresses that attempt to describe the named location, which may be a place name such as "Dalvik, Iceland", an address such as "1600 Amphitheatre Parkway, Mountain View, CA", an airport code such as "SFO", and so forth. The returned addresses should be localized for the locale provided to this class's constructor.

You may specify a bounding box for the search results by including the latitude and longitude of the lower left point and upper right point of the box.

Warning: Geocoding services may provide no guarantees on availability or accuracy. Results are a best guess, and are not guaranteed to be meaningful or correct. Do NOT use this API for any safety-critical or regulatory compliance purposes.

Warning: This API may hit the network, and may block for excessive amounts of time. It's strongly encouraged to use the asynchronous version of this API. If that is not possible, this should be run on a background thread to avoid blocking other operations.

Parameters
locationName String: a user-supplied description of a location This value cannot be null.

maxResults int: max number of addresses to return. Smaller numbers (1 to 5) are recommended Value is 1 or greater

lowerLeftLatitude double: the latitude of the lower left corner of the bounding box Value is between -90D and 90D inclusive

lowerLeftLongitude double: the longitude of the lower left corner of the bounding box Value is between -180D and 180D inclusive

upperRightLatitude double: the latitude of the upper right corner of the bounding box Value is between -90D and 90D inclusive

upperRightLongitude double: the longitude of the upper right corner of the bounding box Value is between -180D and 180D inclusive

Returns
List<Address> a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.

Throws
IllegalArgumentException if locationName is null
IllegalArgumentException if any latitude or longitude is invalid
IOException if there is a failure

getFromLocationName

Added in API level 33
public void getFromLocationName (String locationName, 
                int maxResults, 
                double lowerLeftLatitude, 
                double lowerLeftLongitude, 
                double upperRightLatitude, 
                double upperRightLongitude, 
                Geocoder.GeocodeListener listener)

Returns an array of Addresses that attempt to describe the named location, which may be a place name such as "Dalvik, Iceland", an address such as "1600 Amphitheatre Parkway, Mountain View, CA", an airport code such as "SFO", and so forth. The returned addresses should be localized for the locale provided to this class's constructor.

You may specify a bounding box for the search results by including the latitude and longitude of the lower left point and upper right point of the box.

Warning: Geocoding services may provide no guarantees on availability or accuracy. Results are a best guess, and are not guaranteed to be meaningful or correct. Do NOT use this API for any safety-critical or regulatory compliance purposes.

Parameters
locationName String: a user-supplied description of a location This value cannot be null.

maxResults int: max number of addresses to return. Smaller numbers (1 to 5) are recommended Value is 1 or greater

lowerLeftLatitude double: the latitude of the lower left corner of the bounding box Value is between -90D and 90D inclusive

lowerLeftLongitude double: the longitude of the lower left corner of the bounding box Value is between -180D and 180D inclusive

upperRightLatitude double: the latitude of the upper right corner of the bounding box Value is between -90D and 90D inclusive

upperRightLongitude double: the longitude of the upper right corner of the bounding box Value is between -180D and 180D inclusive

listener Geocoder.GeocodeListener: a listener for receiving results This value cannot be null.

Throws
IllegalArgumentException if locationName is null
IllegalArgumentException if any latitude or longitude is invalid

getFromLocationName

Added in API level 33
public void getFromLocationName (String locationName, 
                int maxResults, 
                Geocoder.GeocodeListener listener)

Provides an array of Addresses that attempt to describe the named location, which may be a place name such as "Dalvik, Iceland", an address such as "1600 Amphitheatre Parkway, Mountain View, CA", an airport code such as "SFO", and so forth. The returned addresses should be localized for the locale provided to this class's constructor.

Warning: Geocoding services may provide no guarantees on availability or accuracy. Results are a best guess, and are not guaranteed to be meaningful or correct. Do NOT use this API for any safety-critical or regulatory compliance purposes.

Parameters
locationName String: a user-supplied description of a location This value cannot be null.

maxResults int: max number of results to return. Smaller numbers (1 to 5) are recommended Value is 1 or greater

listener Geocoder.GeocodeListener: a listener for receiving results This value cannot be null.

Throws
IllegalArgumentException if locationName is null

getFromLocationName

Added in API level 1
Deprecated in API level 33
public List<Address> getFromLocationName (String locationName, 
                int maxResults)

This method was deprecated in API level 33.
Use getFromLocationName(java.lang.String, int, android.location.Geocoder.GeocodeListener) instead to avoid blocking a thread waiting for results.

Returns an array of Addresses that attempt to describe the named location, which may be a place name such as "Dalvik, Iceland", an address such as "1600 Amphitheatre Parkway, Mountain View, CA", an airport code such as "SFO", and so forth. The returned addresses should be localized for the locale provided to this class's constructor.

Warning: Geocoding services may provide no guarantees on availability or accuracy. Results are a best guess, and are not guaranteed to be meaningful or correct. Do NOT use this API for any safety-critical or regulatory compliance purposes.

Warning: This API may hit the network, and may block for excessive amounts of time. It's strongly encouraged to use the asynchronous version of this API. If that is not possible, this should be run on a background thread to avoid blocking other operations.

Parameters
locationName String: a user-supplied description of a location This value cannot be null.

maxResults int: max number of results to return. Smaller numbers (1 to 5) are recommended Value is 1 or greater

Returns
List<Address> a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.

Throws
IllegalArgumentException if locationName is null
IOException if there is a failure

isPresent

Added in API level 9
public static boolean isPresent ()

Returns true if there is a geocoder implementation present on the device that may return results. If true, there is still no guarantee that any individual geocoding attempt will succeed.

Returns
boolean