7

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

4

1 回答 1

5

得到的答案如下:

//这是基于“多边形算法中的点”

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
        } 
    }
于 2013-03-27T10:07:34.283 回答