問題描述
我想知道是否真的有某種方法可以使用 Leaflet.js 在我的位置附近找到標記.我想到的第一個想法是存儲我的位置的 lat 和 lng,然后遍歷一組 lat 和 lng 標記,將它們放在一個數組中,然后對該數組進行排序.我不確定這是否是一個不錯的選擇,因為如果您在地圖上有一百萬個標記需要一段時間.
I was wondering if there is actually some way to find markers near my position using leaflet.js. The first think to come to my head is to store lat and lng of my position and then iterate over an array of lat and lng markers placing them in an array an then sort that array. I am not sure if this is a good option because if you have a million markers in the map is going to take a while.
偽代碼
var myLatLng = [34,56];
var markers = [[20,30],[10,20],[12,-100],[54,90],[-10, -20],[20,20]];
var closests = [];
function findNearestMarker (myPosition, nearestMarkers){
for(var i = 0; i < nearestMarkers.length){
if((nearestMarkers[i][0] - myPosition[0]) < 100 && (nearestMarkers[i][1] - myPosition[1]) < 100 ){
closests.push(nearestMarkers[i])
}
}
}
我實際上是從地圖開始,不知道這么多方法,我也想在這個項目中使用開放的街道地圖,但是,如果有人知道并使用谷歌地圖或其他服務,我將受到歡迎.
I am actually starting with maps and dont know so many approaches, I will also like to use open street maps for this project, but, if someone know and aproach using google maps or another services it will be welcomed.
推薦答案
試試leaflet-knn,它進行最近鄰查找:給定一個點和一堆其他點,它會找到最近的鄰居(正如它在錫上所說的那樣)
Try leaflet-knn, which does nearest-neighbor lookups: given a point and a bunch of other points, it finds the nearest neighbors (as it says on the tin)
這篇關于如何找到最近的標記leaflet.js的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!