我可以找出一个坐标是否在城市内?

Geo*_*Geo 7 google-maps-api-3

假设我有一个LatLng对象,有没有办法检查它是否代表一个城市内的可能位置?我怎样才能获得城市的极限?我正在使用Google Map V3.

Sco*_*ttE 8

你尝试过反向地理编码吗?

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

您可以检查格式化的地址,以查看该城市是否与您要查找的内容相匹配.我不确定这对你的应用程序有多准确,但它是一个选项.

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);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)