I am trying to create lines on a map with Leaflet
between latitude/longitude points. Here is a sample input data:
segment_id latitude1 longitude1 latitude2 longitude2 len
1 1 48.15387 17.07388 48.15396 17.07387 10.98065
2 1 48.15396 17.07387 48.15404 17.07377 11.31327
3 1 48.15404 17.07377 48.15410 17.07364 11.74550
4 1 48.15410 17.07364 48.15412 17.07349 11.48138
5 1 48.15412 17.07349 48.15412 17.07334 11.63625
6 2 48.15424 17.07307 48.15432 17.07299 10.79304
The result of this should be 6 lines lat1,lng1
-> lat2,lng2
. I have a hard time working with addPolylines
, it is creating extra unwanted lines and I am not sure why.
This is how it should look like, without the extra lines stacked on top of each other :D
Here's my code so far but it's garbage:
drawEdges <- function(x) {
d <- cbind(x$latitude1,x$latitude2)
s <- rep(1:nrow(x), each = 2) + (0:1) * nrow(x)
latitudeOut <- d[s]
e <- cbind(x$longitude1,x$longitude2)
t <- rep(1:nrow(x), each = 2) + (0:1) * nrow(x)
longitudeOut <- e[t]
mymap <<- addPolylines(map = mymap,data = x, lng = ~longitudeOut, lat = ~latitudeOut)
}
if (!is.null(edges)){
segments <- split( edges , f = edges$segment_id )
segments
sapply(segments, drawEdges)
}
Thank you for helping