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