如何计算本机地图中多边形的面积?

时间:2019-03-29 10:43:25

标签: react-native react-native-maps

我正在将坐标数组传递给多边形,并希望通过该坐标找到多边形的面积。我已经检查了react-native-maps的文档,但是没有提供任何功能。 有没有办法计算面积。 预先感谢。

1 个答案:

答案 0 :(得分:0)

图书馆不会为您提供此功能。

尝试一下

function calcArea(locations) {

    if (!locations.length) {    
        return 0;
    }
    if (locations.length < 3) {
        return 0;
    }
    let radius = 6371000;

    const diameter = radius * 2;
    const circumference = diameter * Math.PI;
    const listY = [];
    const listX = [];
    const listArea = [];
    // calculate segment x and y in degrees for each point

    const latitudeRef = locations[0].latitude;
    const longitudeRef = locations[0].longitude;
    for (let i = 1; i < locations.length; i++) {
      let latitude = locations[i].latitude;
      let longitude = locations[i].longitude;
      listY.push(this.calculateYSegment(latitudeRef, latitude, circumference));

      listX.push(this.calculateXSegment(longitudeRef, longitude, latitude, circumference));

    }

    // calculate areas for each triangle segment
    for (let i = 1; i < listX.length; i++) {
      let x1 = listX[i - 1];
      let y1 = listY[i - 1];
      let x2 = listX[i];
      let y2 = listY[i];
      listArea.push(this.calculateAreaInSquareMeters(x1, x2, y1, y2));

    }

    // sum areas of all triangle segments
    let areasSum = 0;
    listArea.forEach(area => areasSum = areasSum + area)


    // get abolute value of area, it can't be negative
    let areaCalc = Math.abs(areasSum);// Math.sqrt(areasSum * areasSum);  
    return areaCalc;
}

function calculateAreaInSquareMeters(x1, x2, y1, y2) {
    return (y1 * x2 - x1 * y2) / 2;
}

function calculateYSegment(latitudeRef, latitude, circumference) {
    return (latitude - latitudeRef) * circumference / 360.0;
}

function calculateXSegment(longitudeRef, longitude, latitude, circumference)     {
    return (longitude - longitudeRef) * circumference * Math.cos((latitude * (Math.PI / 180))) / 360.0;
}

Reference