Luc*_*s W 17 google-maps-api-3
我有很多用loadGeoJson加载的多边形特征,我想得到每个的latLngBounds.我是否需要编写一个函数来遍历多边形中的每个lat长对,并在LatLngBounds上为每个对执行extend(),还是有更好的方法?(如果没有,我可能会弄清楚如何迭代多边形顶点,但指向一个示例的指针将是受欢迎的)
Dr.*_*lle 27
Polygon-features没有暴露边界的属性,您必须自己计算它.
例:
//loadGeoJson runs asnchronously, listen to the addfeature-event
google.maps.event.addListener(map.data,'addfeature',function(e){
//check for a polygon
if(e.feature.getGeometry().getType()==='Polygon'){
//initialize the bounds
var bounds=new google.maps.LatLngBounds();
//iterate over the paths
e.feature.getGeometry().getArray().forEach(function(path){
//iterate over the points in the path
path.getArray().forEach(function(latLng){
//extend the bounds
bounds.extend(latLng);
});
});
//now use the bounds
e.feature.setProperty('bounds',bounds);
}
});
Run Code Online (Sandbox Code Playgroud)
演示:http://jsfiddle.net/doktormolle/qtDR6/
在Google Maps JavaScript API v2中,Polygon有一个getBounds()方法,但v3 Polygon不存在这种方法.这是解决方案:
if (!google.maps.Polygon.prototype.getBounds) {
google.maps.Polygon.prototype.getBounds = function () {
var bounds = new google.maps.LatLngBounds();
this.getPath().forEach(function (element, index) { bounds.extend(element); });
return bounds;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19138 次 |
最近记录: |