NodeJS 用于确定坐标位于哪个多边形内部的函数无法正常工作?

brccelvz  于 2022-12-03  发布在  Node.js
关注(0)|答案(1)|浏览(157)

我需要找到/使用一个函数,它接受一个多边形数组,并确定坐标在哪些多边形内。我可以在这个github项目中导入这个函数:https://github.com/mikolalysenko/robust-point-in-polygon
当我使用与网站上完全相同的语法测试函数时,它工作正常(返回-1):

const test = [[-74, 50], [-80, 47], [-74, 45], [-70, 47]]
console.log(classifyPoint(test, [-73.5698065, 45.5031824]))

当我创建一个多边形数组并将函数放入一个循环中时(以便它遍历多边形列表并将其与相同的坐标进行比较),它无法正常工作(返回1):

const zones = [
[[-104, 47], [-100, 47], [-100, 50], [-104, 45]],
[[-74, 50], [-80, 47], [-74, 45], [-70, 47]]
];

function findZone(long, lat) {
    for (i = 0; i < zones.length; i++) {

    const zone = zones[i]
    
    let result = classifyPoint(zone, [long, lat])

    console.log(result)

        if (result === -1 && (i = 0)) {
            return "try again :("
        }
        if (result === -1 && (i = 1)) {
            return "YAHOO"
        }
        else {
            return "womp"
        }
    }
}

console.log(findZone(-73.5698065, 45.5031824))

我不想使用谷歌MapAPI。有人知道为什么它不工作吗?

6pp0gazn

6pp0gazn1#

看起来您不小心指定了i,而不是在此处进行比较:

if (result === -1 && (i = 0)) {
            return "try again :("
        }
        if (result === -1 && (i = 1)) {
            return "YAHOO"
        }

尝试将=更改为===,如下所示:

if (result === -1 && (i === 0)) {
            return "try again :("
        }
        if (result === -1 && (i === 1)) {
            return "YAHOO"
        }

相关问题