Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get City / State / Country for Mapbox using reverse geocoding?

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?

like image 554
somedirection Avatar asked Jan 06 '15 03:01

somedirection


People also ask

Does Mapbox have places API?

The Mapbox Geocoding API allows you to do forward and reverse geocoding operations. Forward Geocoding takes text in the form of an address or place and converts it to geographic coordinates (latitude/longitude).

How do I get latitude and longitude from Mapbox?

If you need to find a location's latitude and longitude, you can use the Mapbox Search playground, which allows you to search for a location and retrieve its coordinates in longitude,latitude format.

Is Google Maps reverse geocoding free?

It's free as long as you credit them and you need fewer than 15000 lookups per day.


1 Answers

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;
                }
like image 188
somedirection Avatar answered Nov 11 '22 15:11

somedirection