如何在谷歌地图v3中获取LatLng的特征多边形几何? [英] How to get LatLngBounds of feature polygon geometry in google maps v3?
问题描述
我有很多加载了loadGeoJson的多边形特征,我希望得到每个特征的latLngBounds。我是否需要编写一个函数来迭代多边形中的每对经纬长对,并在每个LatLngBounds上执行extend(),还是有更好的方法? (如果没有,我可能会弄清楚如何遍历多边形顶点,但是指向这个例子的指针会很受欢迎) 解决方案
p> Polygon-features没有公开边界的属性,你必须自己计算它。
示例:
// loadGeoJson异步运行,监听addfeature-event
google.maps.event.addListener(map.data,'addfeature',function (e){
//检查多边形
if(e.feature.getGeometry()。getType()==='Polygon'){
//初始化边界
var bounds = new google.maps.LatLngBounds();
//遍历路径
e.feature.getGeometry()。getArray() ).forEach(function(path){
//迭代路径中的点
path.getArray()。forEach(function(latLng){
//扩展边界
bounds.extend(的latLng);
});
});
//现在使用边界
e.feature.setProperty('bounds',bounds);
}
});
演示: http://jsfiddle.net/doktormolle/qtDR6/
I have a lot of polygonal features loaded with loadGeoJson and I'd like to get the latLngBounds of each. Do I need to write a function that iterates through every lat long pair in the polygon and does an extend() on a LatLngBounds for each, or is there a better way? (If not, I can probably figure out how to iterate through the polygon vertices but pointers to an example of that would be welcome)
The Polygon-features doesn't have a property that exposes the bounds, you have to calculate it on your own.
Example:
//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);
}
});
Demo: http://jsfiddle.net/doktormolle/qtDR6/
这篇关于如何在谷歌地图v3中获取LatLng的特征多边形几何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!