How Do I connect two coordinates with a line using Leaflet in R

前端 未结 5 1995
半阙折子戏
半阙折子戏 2020-11-28 09:27

I am trying to use Leaflet package in R to draw a amp and connect the markers given the latitude and longitude information in the table below.


    | Observation          


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 09:50

    Depending on what the purpose of the lines is, another great option is gcIntermediate(). It outputs a CURVED SpatialLines object, based on the curvature of the earth. Not great for directions though. SpatialLines class objects work very well with Leaflet. See here for an excellent example. I've posted a modified form, that starts with the data frame from Paul Reiners.

    library(leaflet)
    library(geosphere)
    
    mydf <- data.frame(InitialLat = c(62.469722,48.0975), # initial df
                   InitialLong = c(6.187194, 16.3108),
                   NewLat = c(51.4749, 51.4882),
                   NewLong = c(-0.221619, -0.302621))
    
    p1 <- as.matrix(mydf[,c(2,1)]) # it's important to list lng before lat here
    p2 <- as.matrix(mydf[,c(4,3)]) # and here
    
    gcIntermediate(p1, p2,  
               n=100, 
               addStartEnd=TRUE,
               sp=TRUE) %>% 
    leaflet() %>% 
    addTiles() %>% 
    addPolylines()
    

提交回复
热议问题