测量面积为n个gps坐标

时间:2014-08-04 15:25:49

标签: javascript map gps coordinates

我需要能够使用javascript / jquery测量具有n量gps坐标的区域。我无法使用javascript或n坐标找到这个,所以我决定问是否有人知道如何?

一个例子是我有6个单独的纬度/经度坐标,我需要用它来测量它们的平方米面积。

有任何帮助吗? :)

1 个答案:

答案 0 :(得分:2)

此解决方案仅适用于由纬度/经度点形成的凸多边形。大多数工作是以逆时针顺序重新排列纬度/经度点。重新排序完成后,您可以轻松找到不规则多边形的区域。

//make sure to add the first term to the end of both arrays

//              ---------------SAME---------------
//             |                                  |
var lats = [25.767368, 34.088808, 40.727093, 25.767368];
//             |                                  |
var lons = [-80.18930, -118.40612, -73.97864, -80.18930];

//get the average center point of the polygon
var lats_sum = 0;
var lons_sum = 0; 

for (var i=lats.length; i--;) {
    lats_sum += lats[i];
    lons_sum += lons[i]; 
}

var lat_origin = lats_sum / lats.length; 
var lon_origin = lons_sum / lons.length; 

//translate origin to (0,0) by shifting lat lons
//and calculate the standard angle of the point
var angles = new Array(lats.length); 

for (var j=lats.length; j--;) {
    lats[j] -= lat_origin; 
    lons[j] -= lon_origin; 

    if (lons[j] >= 0 && lats[j] >= 0) {
        angles[j] = Math.abs(Math.atan(lats[j]/lons[j]) * 180 / Math.PI); 
    } else if (lons[j] < 0 && lats[j] >= 0) {
        angles[j] = 90 + Math.abs(Math.atan(lats[j]/lons[j]) * 180 / Math.PI); 
    } else if (lons[j] < 0 && lats[j] < 0) {
        angles[j] = 180 + Math.abs(Math.atan(lats[j]/lons[j]) * 180 / Math.PI); 
    } else if (lons[j] >= 0 && lats[j] < 0) {
        angles[j] = 270 + Math.abs(Math.atan(lats[j]/lons[j]) * 180 / Math.PI);
    }
}

//re-arrange the points from least to greatest angle 
var cur_ang, cur_lat, cur_lon; 

for (var l = 0; l < angles.length; l++) {
    for (var k = 0; k < angles.length - 1; k++) {
        cur_ang = angles[k]; 
        cur_lat = lats[k]; 
        cur_lon = lons[k]; 

        if (cur_ang < angles[k+1]) {
            angles[k] = angles[k+1];
            lats[k] = lats[k+1]; 
            lons[k] = lons[k+1]; 

            angles[k+1] = cur_ang; 
            lats[k+1] = cur_lat; 
            lons[k+1] = cur_lon; 
        }
    }
}

//calculate area for irregular polygon
var sum1 = 0;
var sum2 = 0; 

for (var t = 0; t < lats.length; t++) {

    if (t != lats.length - 1) {

        sum1 += lats[t] * lons[t+1];
        sum2 += lons[t] * lats[t+1]; 

    } else {

        sum1 += lats[t] * lons[0]; 
        sum2 += lons[t] * lats[0]; 

    }
}

var area = (sum1 - sum2) / 2.0;

console.log("Area: " + area); 

然后,当谈到将该纬度区域转换为米平方时,我不知道。这是我在网上找到的一个功能,可以帮助你开始。

// Use to convert from lat long dist to meters
function measure(lat1, lon1, lat2, lon2){  // generally used geo measurement function
    var R = 6378.137; // Radius of earth in KM
    var dLat = (lat2 - lat1) * Math.PI / 180;
    var dLon = (lon2 - lon1) * Math.PI / 180;
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
    Math.sin(dLon/2) * Math.sin(dLon/2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    var d = R * c;
    return d * 1000; // meters
}