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

前端 未结 3 544
轻奢々
轻奢々 2021-01-02 16:37

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

相关标签:
3条回答
  • 2021-01-02 17:04

    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.

    0 讨论(0)
  • 2021-01-02 17:17

    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);
            }
        });
    }
    
    0 讨论(0)
  • 2021-01-02 17:25

    You can use Reverse Geocoding in the Google Geocoder API and check the locality entries and perhaps sublocality entries of results returned. http://code.google.com/apis/maps/documentation/geocoding/#Types

    Note that this probably won't work so well if you have addresses all over the world and want to know "What city is this LatLng in?" On the other hand, it will probably work reasonably well if you want to know "Is this LatLng within Chicago?" There are areas of the world where the data is fairly complete and sensible, and areas where it is incomplete and/or organized in ways you might not expect. (Apparently, the UK uses "county" and "state" very differently from the USA, for example. Even if I'm wrong about that, you get the idea.)

    0 讨论(0)
提交回复
热议问题