Geocode Result
The GeocodeResult
object holds the individual geocode response. It is accessed through the GeocodeResults object.
The purpose of this object is to provide programatic access to the response data instead of having to work with a json-like object.
Example usage
Below is an example of how you could interact with this object when working with geocode results.
const geocoder = G.geocode({
address: '221 B Baker St, London, England'
});
geocoder.fetch()
.then((response) => {
response.getResults().forEach((result) => {
const lat = result.getLatitude();
const lng = result.getLongitude();
});
})
.catch((error) => {
console.error('Error: ', error);
});
Methods
getAddressComponents
getAddressComponents(): GeocodeAddressComponent[]
Get the array of address component objects.
const addressComponents = result.getAddressComponents();
getBounds
getBounds(): LatLngBounds | undefined
Get the precise bounds of the result. If the bounds are not avaiable then undefined
is returned.
const bounds = result.getBounds();
getCompoundPlusCode
getCompoundPlusCode(): string
Get the compound plus code associated with the location.
The compound plus code is a plus code where the first four characters (the area code) are dropped and replaced with a locality description. For example, "9G8F+5W Zurich, Switzerland". If no suitable locality that can be found to shorten the code then this will return an empty string.
const bounds = result.getCompoundPlusCode();
getFormattedAddress
getFormattedAddress(): string
Get the formatted address for the location. If it is not available then an empty string is returned.
const bounds = result.getFormattedAddress();
getLatitude
getLatitude(): number | undefined
Get the latitude for the location. If no latitude is available then undefined
is returned.
const lat = result.getLatitude();
getLocation
getLocation(): LatLng | undefined
Get the LatLng object that holds the latitude and longitude for the location. If no LatLng is available then undefined
is returned.
const latLng = result.getLocation();
getLocationType
getLocationType(): string
Get the location type value. If it is not available then an empty string is returned.
const latLng = result.getLocationType();
getLongitude
getLongitude(): number | undefined
Get the longitude for the location. If no longitude is available then undefined
is returned.
const lng = result.getLongitude();
getPlaceId
getPlaceId(): string
Get the Google Maps place id value for the location. If it is not available then an empty string is returned.
const placeId = result.getPlaceId();
getPlusCode
getPlusCode(): string
Get the plus code for the location. If it is not available then an empty string is returned.
See the Plus Codes website for more information about plus codes.
const placeId = result.getPlusCode();
getPostalCodeLocalities
getPostalCodeLocalities(): string[]
Gets the postal code localities for the location. If none are available then an empty array is returned.
This is only populated when the result is a postal code that contains multiple localities.
const localities = result.getPostalCodeLocalities();
getTypes
getTypes(): GeocodeAddressTypes
Get GeocodeAddressTypes object for the result address types.
const types = result.getTypes();
getTypesArray
getTypesArray(): string[]
Get the array of type values for the result.
This is a shortcut to calling result.getTypes().getTypes()
.
const types = result.getTypesArray();
isLocationApproximate
isLocationApproximate(): boolean
Returns if the location is an approximate location.
if (result.isLocationApproximate()) {
// Do something
}
isLocationGeometricCenter
isLocationGeometricCenter(): boolean
Returns if the location is a geometic center of a result.
if (result.isLocationGeometricCenter()) {
// Do something
}
isLocationRangeInterpolated
isLocationRangeInterpolated(): boolean
Returns if the location is an approximation interpolated between two precise locations.
if (result.isLocationRangeInterpolated()) {
// Do something
}
isLocationRooftop
isLocationRooftop(): boolean
Returns if the location is a rooftop location, which is the most precise location available.
if (result.isLocationRooftop()) {
// Do something
}
isPartialMatch
isPartialMatch(): boolean
Returns if the location is a partial match for the original request.
if (result.isPartialMatch()) {
// Do something
}
toGoogle
toGoogle(): boolean
Get the original Google Maps GeocoderResult object. If the result is empty, an empty object is returned.
const object = result.toGoogle();