我有json格式的四个轮廓(特征),在传单地图上显示为多边形。当前,我在单击L.Control.Locate箭头时正在显示我的当前位置。它显示了我在多边形附近的位置,但是,我想知道我最靠近哪个多边形(按标题),并有一个警报或至少一个变量告诉我最近的标题。

使用下一个最近的https://github.com/mapbox/leaflet-knn,我不清楚如何获得最近的点。我创建了一个小函数:

function nearBy(latlng){
    //does nearBy() have the latlng? This does show me the latlng of the user
    alert(latlng);
    //pass in all the data
    var gj = L.geoJson(GEOJSON_DATA);
    //do the knn magics
    var index = leafletKnn(gj).nearest(L.latLng(latlng), 5);
    //show me something
    alert(index);
}


警报(索引)涌出:[对象对象],[对象对象],[对象对象],[对象对象],[对象对象]

...很好,因为它表明那里有东西,但是,我什么也没得到。

有人在使用leaflet-knn来做我吗?

...编辑...
json看起来像这样(下面不完整):

{ "type": "Feature", "properties": { "Name": "<a href='framework/index.html'><h3>Conecuh National Forest</h3></a>", "description": "<html xmlns:fo=\"http:\/\/www.w3.org\/1999\/XSL\/Format\" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\">\n\n<head>\n\n<META http-equiv=\"Content-Type\" content=\"text\/html\">\n\n<meta http-equiv=\"content-type\" content=\"text\/html; charset=UTF-8\">\n\n<\/head>\n\n<body style=\"margin:0px 0px 0px 0px;overflow:auto;background:#FFFFFF;\">\n\n<table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-collapse:collapse;padding:3px 3px 3px 3px\">\n\n<tr style=\"text-align:center;font-weight:bold;background:#9CBCE2\">\n\n<td>Conecuh National Forest<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>\n\n<table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-spacing:0px; padding:3px 3px 3px 3px\">\n\n<tr>\n\n<td>PROCLAIMEDFORESTID<\/td>\n\n<td>295366010328<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>FORESTNAME<\/td>\n\n<td>Conecuh National Forest<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>GIS_ACRES<\/td>\n\n<td>171215.333<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>SHAPE<\/td>\n\n<td>Polygon<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>SHAPE.AREA<\/td>\n\n<td>0.065501<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>SHAPE.LEN<\/td>\n\n<td>1.596221<\/td>\n\n<\/tr>\n\n<\/table>\n\n<\/td>\n\n<\/tr>\n\n<\/table>\n\n<\/body>\n\n<\/html>", "timestamp": null, "begin": null, "end": null, "altitudeMode": null, "tessellate": 1, "extrude": -1, "visibility": -1, "drawOrder": null, "icon": null }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ -86.399549, 31.205536, 0.0 ], [ -86.407326, 31.205638, 0.0 ], [ -86.409808, 31.205671, 0.0 ], [ -86.413319, 31.205717, 0.0 ], [ -86.414067, 31.205711, 0.0 ], [ -86.418361, 31.205674, 0.0 ], [ -86.429486, 31.20558, 0.0 ], [ -86.429544, 31.198715, 0.0 ], [ -86.429615, 31.190445, 0.0 ], [ -86.429627, 31.185575, 0.0 ], [ -86.429651, 31.175982, 0.0 ], [ -86.430013, 31.175979, 0.0 ], [ -86.430563, 31.175974, 0.0 ], [ -86.432963, 31.175952, 0.0 ], [ -86.433052, 31.175951, 0.0 ], [ -86.433474, 31.175947, 0.0 ], [ -86.433864, 31.175944, 0.0 ]


...编辑2 ...

使用FireBug和console.log(),我可以得到结果。 Leaflet-knn正确返回它们,并具有最接近的LatLng作为(在我的json中)index [0] .lat +“,” index [0] .lon

如果有人,我仍然会喜欢一些“干净”的方法示例...

最佳答案

使用nearestLayer代替nearest:这将返回最近的层而不是文档中指定的最近点。

关于javascript - 使用Leaflet JS返回最近要素(json)多边形的标题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24295651/

10-17 00:04