2011-12-21 248 views
2

我想在地图上标记邮政编码的大纲(界限)。借助Google地图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();     
     } 
    } 

回答

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,因为事实并非总是如此。

+0

这实际上产生一个矩形。至少在荷兰,它的确如此。我希望为邮政编码的多边形设置界限,请尝试'3335,NL',有没有想法如何实现这一点? – 2012-09-15 16:44:21

+0

不,我不相信您可以从Google Maps API获取这类信息。你会得到的是一个全球范围的区域,你所搜索的位置适合。查看官方文档:https://developers.google.com/maps/documentation/javascript/geocoding 更多信息:http://stackoverflow.com/questions/9491114/google-maps-api-v3 -geocoder-结果的问题 - 有界 – MrUpsidown 2012-09-23 16:48:48

0

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

enter image description here功能DisplayZipCodeArea(边界,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 

}

相关问题