identify which is the polygon from latitude and longitude

大憨熊 提交于 2020-01-02 03:12:10

问题


I have a map drawn using leaflet.js. If I give longitude and latitude value as input Can I identify the polygon? Can I get a client side script for this?


回答1:


Got an answer as follows :

//This is based on '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
        } 
    }


来源:https://stackoverflow.com/questions/15652424/identify-which-is-the-polygon-from-latitude-and-longitude

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!