我的Google地图上有一个多边形工具和一个形状工具。我希望用户能够使用这两种工具计算形状的面积。问题是,对于大小相同(或非常接近)的形状,我得到的区域值大不相同。例如,多边形区域结果将是552523.74,而矩形区域结果将是27.57。
到目前为止,这是我的代码: 的矩形
google.maps.event.addListener(newShape, 'bounds_changed', function() {
var area= rectArea(newShape.getBounds());
}
function rectArea(bounds){
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast();
var southWest=new google.maps.LatLng(sw.lat(), sw.lng());
var northEast=new google.maps.LatLng(ne.lat(), ne.lng());
var southEast = new google.maps.LatLng(sw.lat(),ne.lng());
var northWest = new google.maps.LatLng(ne.lat(),sw.lng());
console.log('rec coords: '+ northEast +',' + northWest +',' + southEast +',' + southWest);
area=google.maps.geometry.spherical.computeArea([northEast,northWest,southEast,southWest]);
return area;
}
多边形
google.maps.event.addListener(drawingManager, 'polygoncomplete', function (polygon) {
var coordinates = (polygon.getPath().getArray());
area=google.maps.geometry.spherical.computeArea(coordinates);
console.log('poly coords: '+ coordinates);
console.log('Polygon '+ this.id +' Area is: ' + area);
});
答案 0 :(得分:2)
用于计算矩形区域的数组的顺序错误,必须是例如:
[northEast,northWest,southWest,southEast]
示范差异: