2016-10-17 40 views
0

在我的示例中,我試圖檢查標記是否在邊界框內。如果是,則彈出文本被設置爲true。如何用Leaflet檢查標記是否在邊界框中

我一直以「L.latlngBounds」結尾,不是一個函數。 會有人能夠指出我在正確的方向嗎?

checkBounds = (marker) -> 
    if L.latlngBounds(inBounds).contains(currentMarker.getLatLng()) 
    return "True" 
    else 
    return "False" 

map = L.map('mapid').setView([ 
    51.505 
    -0.09 
], 13) 
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors').addTo map 

neCorner = L.marker([47.6349, -122.3206]) 
swCorner = L.marker([47.6341, -122.3211]) 
currentMarker = L.marker([47.6345, -122.3208]) 
inBounds = new L.featureGroup([swCorner, neCorner]) 

map.fitBounds(inBounds.getBounds(), { padding: [50, 50] }) 
currentMarker.addTo(map).bindPopup(checkBounds(currentMarker)).openPopup() 

更新 無法弄清楚如何評論發表的代碼,所以我會做在這裏

checkBounds = (marker) -> 
    if L.latLngBounds([swCorner, neCorner]).contains(marker) 
    return "True" 
    else 
    return "False" 

map = L.map('mapid').setView([ 
    51.505 
    -0.09 
], 13) 
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors').addTo map 

neCorner = L.latLng([47.6349, -122.3206]) 
swCorner = L.latLng([47.6341, -122.3211]) 

currentMarker = L.latLng([47.6355, -122.3208]) 

map.fitBounds(([swCorner, neCorner]), { padding: [50, 50] }) 
L.marker(currentMarker).addTo(map).bindPopup(checkBounds(currentMarker)).openPopup() 

回答

2

這是大號納克不LNG,你有一個錯字

L.lat ngBounds VS L.lat 大號 ngBounds

+0

我猜那些新的漸進鏡片並不如我想象的那麼好。 :)完全錯過了「L」。謝謝。對於那些關心的人來說,新代碼的讀取方式如下: – Ben

+0

應該添加了@alex,但在嘗試備份時做了一些有趣的事情。 – Ben

相關問題