2

我想在地图上标记邮政编码的轮廓(边界)。使用谷歌地图 API,我可以发送邮政编码或地址并返回日志/纬度,然后在地图上放置一个图标。现在我想在邮政编码覆盖的整个区域周围制作一个盒子或多边形。是否有 API 或方法可以做到这一点?如果有的话,我可以使用谷歌地图或其他服务。

Api 获取邮政编码的纬度/经度...

if (geocoder)           {       
    geocoder.geocode({ 'address': address }, function (results, status) {                  
        if (status == google.maps.GeocoderStatus.OK){
            var pcode = results[0].address_components[0].long_name;
            var latitude = results[0].geometry.location.lat();
            var longitude = results[0].geometry.location.lng();                 
        }
    }
4

2 回答 2

1
geocoder.geocode({ 'address': address }, function (results, status) {

    if (status == google.maps.GeocoderStatus.OK) {

        var pcode = results[0].address_components[0].long_name;
        var latitude = results[0].geometry.location.lat();
        var longitude = results[0].geometry.location.lng();

        //do whatever you want above then call the displayBounds function
        displayBounds(results[0].geometry.bounds);
    }
});

function displayBounds(bounds) {

    var rectangleOptions = {
        strokeColor: '#0000ff',
        strokeOpacity: 0.5,
        strokeWeight: 3,
        bounds: bounds
    }

    var rectangle = new google.maps.Rectangle(rectangleOptions);

    rectangle.setMap(map); //map being your google.maps.Map object
}

这样您就可以在地图上显示边界。不过,请确保您获得了结果的 geometry.bounds,因为并非总是如此。

于 2012-02-29T02:27:34.253 回答
0

DisplayZipCodeArea(results[0].geometry.bounds, resultsMap);

在此处输入图像描述

function DisplayZipCodeArea(bounds, resultsMap) {

    var rectangleOptions = {
        strokeColor: '#0000ff',
        strokeOpacity: 0.5,
        strokeWeight: 3,
        bounds: bounds
    }

    var rectangle = new google.maps.Rectangle(rectangleOptions);

    rectangle.setMap(resultsMap); //map being your google.maps.Map object
}
于 2017-01-19T19:09:30.143 回答