0

I have X facilities and Y facilities with their lat/long and I have mapped them with leaflet. Problem is how can I limit only Ys within say 25miles of X locations?

I have tried serching but not finding much that addresses my issue or at least I don't think does.

Z<-leaflet() %>% addTiles() %>% addMarkers(lng = Y$longitude, lat = 
  Y$latitude, icon = YIcon, group = "BASE")%>% addMarkers(lng = 
  X$Longitude, lat = X$Latitude, icon = XIcon)
Z
Kyle Overton
  • 49
  • 1
  • 7

2 Answers2

0

Unless I missed something in your question, you just have to measure the distances using https://leafletjs.com/reference-1.5.0.html#map-distance and create/display the markers only if they match your constraint

YaFred
  • 9,698
  • 3
  • 28
  • 40
  • I am having issues because I don't see the syntax used with the distance dunction? I have the lat and long seperate what is the syntax and how do I use the function? – Kyle Overton Sep 09 '19 at 15:42
  • It's a method on your map object. Can you give a complete example ? – YaFred Sep 09 '19 at 15:48
  • The distance function is asking for latlng I have X$Latiture and Y$Latitude as well as their corosponding Longitude I do not see how to use the distance function without code help with syntax on how to use. – Kyle Overton Sep 09 '19 at 16:03
  • You must have created a map object (var mymap = L.map(...)) ... you use this object to calculate distances: mymap.distance(L.latLng(XLat, XLon), L.latLng(YLat, YLon)) – YaFred Sep 09 '19 at 17:02
0

I think you can solve the problem by performing some spatial operations before using leaflet. All of the needed functions are in the sf library

To ge the Ys facilitities within 25 miles of X facilities you have to:

  • Tranform X and Y to a Projected projection. Use the st_transform() function.
  • Calculate a buffer for X. Let´s say Xbuffer <- st_buufer(X, 40200). The units are in meters.
  • Select the Y points in Xbuffer with the st_join() function and a an additional filter. Here you create a new object (Y2).
  • Use leaflet to plot X and Y2.

Please notice that a reproducible example could help to give a better answer. You may find some additional help in the spatial operations here

Orlando Sabogal
  • 1,470
  • 7
  • 20
  • or even a simple haversine formula if you don't want to include an additional library (https://stackoverflow.com/questions/14560999/using-the-haversine-formula-in-javascript) – YaFred Sep 09 '19 at 15:29