8
votes

I've read this API and tested out a lot of coordinates based on some of the examples https://www.mapbox.com/developers/api/geocoding/

is an example that gives a lot of information, the problem is there's no easy way to get just the city, state, and country that I've been able to find.

http://api.tiles.mapbox.com/v4/geocode/mapbox.places/-114.0701,51.0495.json?access_token=pk.eyJ1IjoibWFyaXNhZmx5bm4iLCJhIjoibG9JcmhrbyJ9.yDc_eDeDW2DeM_JVSQPp7g

I can get results like this

Mc Lean, 22102, Virginia, United States

San Francisco, 94103, California, United States

London, SW1H6, City of Westminster, United Kingdom

San Francisco, 94114, California, United States

Perth, 6053, Western Australia, Australia

The problem is then I'd have to figure out how to parse out postal codes for each country (not reasonable).

I feel that http://api.tiles.mapbox.com/v4/geocode/mapbox.places/ might be the wrong endpoint. I found http://api.tiles.mapbox.com/v4/geocode/mapbox.places-country-v1/ at one point but it will only give the country name back.

Any guidance how to get city/state/country data from a Mapbox reverse geocoding api lookup?

2

2 Answers

7
votes

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;
                }
0
votes

Same as Above. When i use to set the input fields from the geocoder Result Data, i will use this format.

        placeName = geoData.place_name.split(',');
        $('#addressLine1').val(placeName[1]);

            if(v.id.indexOf('region') >= 0) {
                region = v.text;
                $('input[name="vstate"]').val(region);
            }
            if(v.id.indexOf('postcode') >= 0) {
                postcode = v.text;
                $('input[name="vpostalCode"]').val(postcode);
            }
            if(v.id.indexOf('place') >= 0) {
                city = v.text;
                $('input[name="vcity"]').val(city);
            }               
            if(v.id.indexOf('country') >= 0) {
                countryName = v.text;
                if (countryName.indexOf('United States') > 0) {
                    $('input[name="vcountry"]').val("US");
                }
                if (countryName.indexOf('Canada') > 0) {
                    $('input[name="vcountry"]').val("CA");
                }
                else {
                    $('input[name="vcountry"]').val("US");
                }
            }