Javascript判断两个点(经纬度)的距离,以及是否在某个区域内(经纬度字符串)

发布于 2020-11-11  75 次阅读


JS计算两个点(经纬度)的距离

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;    // 公里数
}

JS判断某个点是否在某个区域(经纬度)内

/**
 * 预处理判断 某个点是否在某个区域
 * @param {*} lng 
 * @param {*} lat 
 * @param {*} points such as '120.133446,30.271645||120.133153,30.271669||120.133138,30.271595||'
 */
function preceedPointInArea(lng, lat, points) {
    var parking_gps = points
    var gpsStringArray = parking_gps.split('||')
    var polygon = []
    for (var index in gpsStringArray) {
        var item = gpsStringArray[index]
        var point = item.split(',')
        if (point.length == 2) {
            point['lng'] = parseFloat(point[0])
            point['lat'] = parseFloat(point[1])
            polygon.push(point)
        }
    }
    var flag = isPointInPolygon(polygon, lng, lat)

    return flag;
}

/**
 * 某个点是否在某个区域
 */
function isPointInPolygon(polygon, lng, lat) {

    var numberOfPoints = polygon.length;
    var polygonLats = [];
    var polygonLngs = [];
    for (var i = 0; i < numberOfPoints; i++) {
        polygonLats.push(polygon[i]['lat']);
        polygonLngs.push(polygon[i]['lng']);
    }

    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;
}

一沙一世界,一花一天堂。君掌盛无边,刹那成永恒。