1

我的谷歌地图上有一个多边形工具和一个形状工具。我希望用户能够使用这两种工具计算形状的面积。问题是我在相同大小(或非常接近)的形状上获得了截然不同的面积值。例如,多边形面积结果将为 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);
    });
4

1 回答 1

2

用于计算矩形面积的数组有错误的顺序,它必须是例如:

[northEast,northWest,southWest,southEast]

差异的证明:
在此处输入图像描述

于 2013-05-01T07:35:36.780 回答