mf-google-geocoder
v1.2.6
Published
JS library that uses Google Geocoding API and parses the response into a flat, readable and minimal object.
Downloads
167
Readme
mf-google-geocoder
JS library that uses Google Geocoding API (https only) and parses the response into a flat, readable and minimal object.
Usage Example
const address = '672 Franklin Ave #1fl, Brooklyn, NY 11238, USA';
try {
const result = await fromAddressText(address, { apiKey, mfAutoFix: true });
console.log(result.city)
console.log(result.location)
console.log(result.zip)
} catch (e) {
if (e.name === 'MissingAddressDetailsError') { // or check if instanceof
console.log(e.missingTypes);
}
}
Functions and Interfaces
From address text
Gets text and returns AddressDetails
interface and returns AddressDetails
interface
via
function fromAddressText(addressText: string, options: Options)
From Google Geocoder response
Gets GoogleGeoCodeResponse
and returns AddressDetails
interface
via
function fromGoogleGeoCode(googleGeoCodeResponse: GoogleGeoCodeResponse, options: Options)
Returned Value
interface AddressDetails {
/** geometry.location */
location: Geo;
/** address component with 'country' and 'political' types */
country: string;
/** address component with 'administrative_area_level_1' and 'political' types */
state: string;
/** address component with 'administrative_area_level_2' and 'political' types */
county: string;
/** address component with 'neighborhood \ sublocality \ locality' and 'political' types */
city: string;
/** address component with 'route' type */
street: string;
/** address component with 'street_number' type */
streetNumber: string | null;
/** address component with 'postal_code' type */
zip: string;
/** address component with 'postal_code_suffix' type */
zipSuffix: string | null;
/** formatted_address */
fullAddress: string;
/** address component with 'subpremise' type */
address2: string | null;
/** original google API response */
googleGeoCodeResponse: GoogleGeoCodeResponse;
}
MissingAddressDetailsError
:
Throws MissingAddressDetailsError
with missingTypes: (AddressType | GeocodingAddressComponentType)[]
if one of the required fields are missing.
options
interface Options {
/** google API key */
apiKey: string,
/** indicates if to change the original address if fields are missing. Default is true */
mfAutoFix?: boolean;
}