Last active
October 30, 2019 17:12
-
-
Save csandman/d478c4c7bde8895a9bdfb4861753646a to your computer and use it in GitHub Desktop.
Convert the address components from a google places api request to a readable format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ---------------------------------------------------------------------- // | |
// https://developers.google.com/maps/documentation/geocoding/intro#Types // | |
// ---------------------------------------------------------------------- // | |
// returns: | |
// { | |
// address_1 | |
// address_2 | |
// city | |
// state_code | |
// zip_code | |
// country | |
// } | |
function convertAddressComponents(addrComp) { | |
let newAddr = {}; | |
let address_1 = []; | |
addrComp.forEach((el, i) => { | |
if (el.types.includes("post_box")) { | |
address_1.push(el.long_name); | |
} else if (el.types.includes("street_number")) { | |
address_1.push(el.long_name); | |
} else if (el.types.includes("route")) { | |
address_1.push(el.long_name); | |
} else if (el.types.includes("subpremise")) { | |
newAddr.address_2 = el.long_name; | |
} else if (el.types.includes("locality")) { | |
newAddr.city = el.long_name; | |
} else if (el.types.includes("administrative_area_level_1")) { | |
newAddr.state_code = el.short_name; | |
} else if (el.types.includes("postal_code")) { | |
newAddr.zip_code = el.short_name; | |
} else if (el.types.includes("country")) { | |
newAddr.country = el.long_name; | |
} | |
}); | |
newAddr.address_1 = address_1.join(" "); | |
if (!newAddr.city) { | |
const neighborhood = addrComp.find(comp => { | |
return comp.types.includes("neighborhood"); | |
}); | |
if (neighborhood) { | |
newAddr.city = neighborhood.long_name; | |
} | |
} | |
return newAddr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment