Ben Ben - 1 month ago 12
CoffeeScript Question

How to check if a marker is in a bounding box with Leaflet

In my example I'm trying to check if the marker is within the bounding box. If it is, the popup text gets set to true.

I keep ending up with "L.latlngBounds" is not a function.
Would somebody be able to point me in the correct direction?

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()


update
couldn't figure out how to post code in a comment, so I'll do it here

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()

Answer

It's Lng not lng, you have a typo

L.latlngBounds vs L.latLngBounds