javascript – 从纬度和经度中识别哪个是多边形

前端之家收集整理的这篇文章主要介绍了javascript – 从纬度和经度中识别哪个是多边形前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一张使用leaflet.js绘制的地图.如果我给出经度和纬度值作为输入
我可以识别多边形吗?我可以为此获得客户端脚本吗?

解决方法

得到的答案如下:

//这是基于’多边形点算法’

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相关文章