首页 > Web开发 > 详细

js版根据经纬度计算多边形面积(墨卡托投影)

时间:2016-01-03 23:49:44      阅读:780      评论:0      收藏:0      [点我收藏+]

[摘要:var earthRadiusMeters = 6371000.0; var metersPerDegree = 2.0 * Math.PI * earthRadiusMeters / 360.0; var radiansPerDegree = Math.PI / 180.0; var degr] 

var earthRadiusMeters = 6371000.0;
var metersPerDegree = 2.0 * Math.PI * earthRadiusMeters / 360.0;
var radiansPerDegree = Math.PI / 180.0;
var degreesPerRadian = 180.0 / Math.PI;
var pointArr;

$(document).ready(function() {
	pointArr = new Array();
	b();
});


function calculateArea(points) {
	if (points.length > 2) {
		var areaMeters2 = PlanarPolygonAreaMeters2(points);
		if (areaMeters2 > 1000000.0) {
			areaMeters2 = SphericalPolygonAreaMeters2(points);
			alert("面积为" + areaMeters2 + "平方米");
		}
	}
}

/*球面多边形面积计算*/
function SphericalPolygonAreaMeters2(points) {
	var totalAngle = 0;
	for (var i = 0; i < points.length; i++) {
		var j = (i + 1) % points.length;
		var k = (i + 2) % points.length;
		totalAngle += Angle(points[i], points[j], points[k]);
	}
	var planarTotalAngle = (points.length - 2) * 180.0;
	var sphericalExcess = totalAngle - planarTotalAngle;
	if (sphericalExcess > 420.0) {
		totalAngle = points.length * 360.0 - totalAngle;
		sphericalExcess = totalAngle - planarTotalAngle;
	} else if (sphericalExcess > 300.0 && sphericalExcess < 420.0) {
		sphericalExcess = Math.abs(360.0 - sphericalExcess);
	}
	return sphericalExcess * radiansPerDegree * earthRadiusMeters * earthRadiusMeters;
}

/*角度*/
function Angle(p1, p2, p3) {
	var bearing21 = Bearing(p2, p1);
	var bearing23 = Bearing(p2, p3);
	var angle = bearing21 - bearing23;
	if (angle < 0) {
		angle += 360;
	}
	return angle;
}


/*方向*/
function Bearing(from, to) {
	var lat1 = from[1] * radiansPerDegree;
	var lon1 = from[0] * radiansPerDegree;
	var lat2 = to[1] * radiansPerDegree;
	var lon2 = to[0] * radiansPerDegree;
	var angle = -Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lon1 - lon2));
	if (angle < 0) {
		angle += Math.PI * 2.0;
	}
	angle = angle * degreesPerRadian;
	return angle;
}

/*平面多边形面积*/
function PlanarPolygonAreaMeters2(points) {
	var a = 0;
	for (var i = 0; i < points.length; ++i) {
		var j = (i + 1) % points.length;
		var xi = points[i][0] * metersPerDegree * Math.cos(points[i][1] * radiansPerDegree);
		var yi = points[i][1] * metersPerDegree;
		var xj = points[j][0] * metersPerDegree * Math.cos(points[j][1] * radiansPerDegree);
		var yj = points[j][1] * metersPerDegree;
		a += xi * yj - xj * yi;
	}
	return Math.abs(a / 2);
}

function b() {
	var s = "112.523197631836,37.868892669677734;112.5170669555664,37.8605842590332;112.52099609375,37.849857330322266;112.54137420654297,37.8512732521875;112.5351180302734,37.858699798583984";
	var s1 = new Array()
	s1 = s.split(";");
	for (var i = 0; i < s1.length; i++) {
		var ss = s1[i];
		var temp = ss.split(",");
		var point = new Array();
		point.push(Number(temp[0]), Number(temp[1]));
		pointArr.push(point);
	}
	calculateArea(pointArr);
}

 

 

js版根据经纬度计算多边形面积(墨卡托投影)

原文:http://www.cnblogs.com/grimm/p/5097383.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!