javascript算法工具

两点距离

function getGreatCircleDistance(lat1, lng1, lat2, lng2) {
    var EARTH_RADIUS = 6378137.0; 
    var PI = Math.PI;
 
    function getRad(d) {
        return d * PI / 180.0;
    }
    var radLat1 = getRad(lat1);
    var radLat2 = getRad(lat2);
 
    var a = radLat1 - radLat2;
    var b = getRad(lng1) - getRad(lng2);
 
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000.0;
 
    return s;    // 公里数
}

判断当前点是否在多边形内

		/**
			 * @param {Object} polygon 数组
			 * @param {Object} lng 	经度
			 * @param {Object} lat	纬度
			 */
			isPointInPolygon(polygon, lng, lat) {
			 
			    var numberOfPoints = polygon.length;
			    var polygonLats = [];//纬度
			    
			    var polygonLngs = [];//经度
			    for (var i = 0; i < numberOfPoints; i++) {
			        polygonLats.push(polygon[i]['latitude']);
			        polygonLngs.push(polygon[i]['longitude']);
			    }
			 
			    var polygonContainsPoint = false;
			    for (var node = 0, altNode = (numberOfPoints - 1); node < numberOfPoints; altNode = node++) {
			        if ((polygonLngs[node] > lng != (polygonLngs[altNode] > lng))
  && (lat < (polygonLats[altNode] - polygonLats[node])  * (lng - polygonLngs[node])    / (polygonLngs[altNode] - polygonLngs[node])    + polygonLats[node]
			            )
			        ) {
			            polygonContainsPoint = !polygonContainsPoint;
			        }
			    }
			 
			    return polygonContainsPoint;
			}

以上转载https://blog.csdn.net/diding4327/article/details/102428771

你可能感兴趣的:(nodeJS)