如何使用Leaflet检查标记是否在边界框中

时间:2016-10-17 17:48:37

标签: coffeescript leaflet

在我的例子中,我试图检查标记是否在边界框内。如果是,则弹出文本设置为true。

我一直以" 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()

1 个答案:

答案 0 :(得分:2)

L ng lng ,您有错字

L.lat l ngBounds vs L.lat L ngBounds