Skip to content

Instantly share code, notes, and snippets.

@lyatziv
Last active November 14, 2017 23:19
Show Gist options
  • Save lyatziv/a3a65d5e6c61ff45dd56e6b59a5d1a52 to your computer and use it in GitHub Desktop.
Save lyatziv/a3a65d5e6c61ff45dd56e6b59a5d1a52 to your computer and use it in GitHub Desktop.
ziptastic or zippopotam.us alternative
fetch('https://maps.googleapis.com/maps/api/geocode/json?address=10030&region=US', {
method: 'get'
}).then((response) => response.json())
.then((response) => {
let address = response.results[0].address_components.reduce((comps, comp) => {
let attr = comp.types[0];
attr = (attr === 'administrative_area_level_2') ? 'county' : attr;
attr = (attr === 'administrative_area_level_1') ? 'state' : attr;
comps[attr + '_long'] = comp.long_name;
comps[attr + '_short'] = comp.short_name;
return comps;
}, {});
console.log('address ', address);
}).catch(function (err) {
console.log("Error: ", err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment