【问题标题】:How calulate the area of a polygon in react-native-maps?如何计算 react-native-maps 中多边形的面积?
【发布时间】:2023-03-10 19:03:01
【问题描述】:

我将坐标数组传递给多边形,并希望通过该坐标找到多边形的面积。我检查了 react-native-maps 的文档,但没有提供任何功能。 有什么方法可以计算面积。 提前致谢。

【问题讨论】:

    标签: react-native react-native-maps


    【解决方案1】:

    库不会为您提供此功能。

    试试这个

    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

    【讨论】:

    • 只要把这个数组传入函数calcArea()
    • 这个解决方案对我有用,但是当我通过互联网检查该区域时,我的答案与互联网答案之间存在大约一英亩的差异,可能是什么原因?非常感谢您的解决方案。
    • 通过取圆周的常数值解决了我的问题。
    猜你喜欢
    • 2022-07-26
    • 2013-11-13
    • 2012-04-07
    • 2013-05-21
    • 2013-10-24
    • 2010-10-01
    • 2020-01-24
    • 2013-09-07
    相关资源
    最近更新 更多