How to get City / State / Country for Mapbox using reverse geocoding?
I ended up coming up with this function based on what mapbox returned. The root node gives the town/city, and then gives the context around it. It was a matter of checking which context was the postal code and excluding that, while rebuilding the string.
//builds proper format of location string based on mapbox data. city,state/province,country
function parseReverseGeo(geoData) {
// debugger;
var region, countryName, placeName, returnStr;
if(geoData.context){
$.each(geoData.context, function(i, v){
if(v.id.indexOf('region') >= 0) {
region = v.text;
}
if(v.id.indexOf('country') >= 0) {
countryName = v.text;
}
});
}
if(region && countryName) {
returnStr = region + ", " + countryName;
} else {
returnStr = geoData.place_name;
}
return returnStr;
}