0

I have added two markers on leaflet map, and I want to link those markers (like travelling from one location to the other). How can I do that?

The code that I used to create markers is provided below:

p <- leaflet()
p <- p %>%
  addTiles()
p <- p %>% 
  addMarkers(lat = 32.051960, lng = 118.778030)
p <- p %>%
  addMarkers(lat = 33.6028, lng = 73.0646)
p

1 Answers1

0

You can use the gcIntermediate function from the geosphere package to create curved lines in leaflet. You can use the following code:

library(leaflet)
library(dplyr)
library(geosphere)
gcIntermediate(c(118.778030, 32.051960), c(73.0646, 33.6028),
               n=100, 
               addStartEnd=TRUE,
               sp=TRUE) %>% 
  leaflet() %>% 
  addTiles() %>% 
  addMarkers(lat = 32.051960, lng = 118.778030) %>%
  addMarkers(lat = 33.6028, lng = 73.0646) %>%
  addPolylines()
Quinten
  • 35,235
  • 5
  • 20
  • 53
  • Firstly, thank you very much for your response. Can you please explain your code a bit. why is the 'group' variable needed. If I remove 'group = ~group' I get the same response, just that the line in a bit lighter. Secondly, I am new to leaflet and learning this was very exciting for me. However, this was not what I exactly wanted. I wanted lines over the top (line travelling via airlines). Is it possible to do here? – Salman Virani Apr 30 '22 at 21:27
  • @SalmanVirani, I changed my code to curved lines using the `geosphere` package for in `leaflet` – Quinten May 01 '22 at 10:39
  • Thanks for helping me out in the leaflet package – Salman Virani May 18 '22 at 18:53
  • No problem! Always good to update your packages. – Quinten May 18 '22 at 19:01
  • Hi, Quinten, I am actually new to stack overflow. I recently got two negative points on a question, and after that I am unable to ask any question. Can you please help out in any way if possible. – Salman Virani May 18 '22 at 19:05