問題描述
我想知道是否真的有某種方法可以使用 Leaflet.js 在我的位置附近找到標(biāo)記.我想到的第一個(gè)想法是存儲(chǔ)我的位置的 lat 和 lng,然后遍歷一組 lat 和 lng 標(biāo)記,將它們放在一個(gè)數(shù)組中,然后對(duì)該數(shù)組進(jìn)行排序.我不確定這是否是一個(gè)不錯(cuò)的選擇,因?yàn)槿绻诘貓D上有一百萬個(gè)標(biāo)記需要一段時(shí)間.
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])
}
}
}
我實(shí)際上是從地圖開始,不知道這么多方法,我也想在這個(gè)項(xiàng)目中使用開放的街道地圖,但是,如果有人知道并使用谷歌地圖或其他服務(wù),我將受到歡迎.
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,它進(jìn)行最近鄰查找:給定一個(gè)點(diǎn)和一堆其他點(diǎn),它會(huì)找到最近的鄰居(正如它在錫上所說的那樣)
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)
這篇關(guān)于如何找到最近的標(biāo)記leaflet.js的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!