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

前端 未结 3 545
轻奢々
轻奢々 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: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);
            }
        });
    }
    

提交回复
热议问题