Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I find out if a coordinate is inside a city?

Let's say I have a LatLng object, is there any way of checking if it represents a possible location within a city? How can I get the limits of a city? I'm using Google Map V3.

like image 312
Geo Avatar asked Jun 07 '11 17:06

Geo


2 Answers

Have you tried reverse geocoding?

http://code.google.com/apis/maps/documentation/javascript/services.html#ReverseGeocoding

You could check the formatted address to see if the city matches what you're looking for. I'm not sure how accurate this will be for your application, but it's an option.

function codeLatLng() {
    var geocoder = new google.maps.Geocoder(),
        latlng = new google.maps.LatLng(40.730885, -73.997383);
    geocoder.geocode({ 'latLng': latlng }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
                console.log(results[1]);
                console.log(results[1].formatted_address);
            }
        } else {
            alert("Geocoder failed due to: " + status);
        }
    });
}
like image 100
ScottE Avatar answered Oct 02 '22 14:10

ScottE


City limits and other municipalities are constantly re-drawn. There are certain services that exist to help you find them, but I'm not positive that Google keeps a record of city limits inside their data for the Google maps. Here's a discussion in google groups about it. A snippet from that site:

Depending where you are in the world, city limits and other administrative boundaries are CONSTANTLY being changed, and it's even sometimes difficult for local governments to keep their data current because of annexations and other changes. Also, 'city' might be something relative small or the size of Shanghai. Also, different countries can also have sometimes conflicting definitions what administrative unit is the actual definition - a good example is China. Probably your best approach is to get your data from a local government or a data supplier and build your own.

like image 31
NateDSaint Avatar answered Oct 02 '22 15:10

NateDSaint