gpt4 book ai didi

javascript - 根据纬度和经度确定哪个是多边形

转载 作者:数据小太阳 更新时间:2023-10-29 04:44:28 24 4
gpt4 key购买 nike

我有一张使用 leaflet.js 绘制的 map 。如果我将经度和纬度值作为输入我可以识别多边形吗?我可以为此获取客户端脚本吗?

最佳答案

得到的答案如下:

//这是基于'point in polygon algorithm'

function getPoint () {
float x=-89.82421875; //x and y represents the lat and lng values
float y= 40.18307014852533;
var a = boundaries; //the coordinates used to draw the map
for (i = 0; i < a.features.length; i++) {
PointInPolygon(x,y, a.features[i].geometry.coordinates[0], i);
}
};

function PointInPolygon(pointX, pointY, _vertices, number) {
var j = _vertices.length - 1;
var oddNodes = false;
var polyX, polyY,polyXJ,polyYJ;

for (var i = 0; i < _vertices.length; i++) {
polyY = parseFloat(_vertices[i].toString().split(",")[1]);
polyX = parseFloat(_vertices[i].toString().split(",")[0]);
polyXJ = parseFloat(_vertices[j].toString().split(",")[0]);
polyYJ = parseFloat(_vertices[j].toString().split(",")[1]);
if (polyY < pointY && polyYJ >= pointY ||
polyYJ < pointY && polyY >= pointY) {
if (polyX +
(pointY - polyY) / (polyYJ - polyY) * (polyXJ - polyX) < pointX)
{
oddNodes = !oddNodes;
}
}
j = i;
}
if (oddNodes == true) {
map._layers[number+1].fire('click'); //fire the map click event
}
}

关于javascript - 根据纬度和经度确定哪个是多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15652424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com