JS library that uses Google Geocoding API and parses the response into a flat, readable and minimal object.
npm install mf-google-geocoderjavascript
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
$3
Gets text and returns `AddressDetails` interface and returns `AddressDetails` interface
via
`javascript
function fromAddressText(addressText: string, options: Options)
`$3
Gets `GoogleGeoCodeResponse` and returns `AddressDetails` interface
via
`javascript
function fromGoogleGeoCode(googleGeoCodeResponse: GoogleGeoCodeResponse, options: Options)
`$3
`javascript
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;
}
`$3
Throws `MissingAddressDetailsError` with `missingTypes: (AddressType | GeocodingAddressComponentType)[]`
if one of the required fields are missing.
$3
`javascript
interface Options {
/* google API key /
apiKey: string,
/* indicates if to change the original address if fields are missing. Default is true /
mfAutoFix?: boolean;
}
``