Mapview highlight SpatialLines upon hover

330 Views Asked by At

I want to highlight all lines going to a node/marker on a map in mapview. In the example code here, the nodes represent capital cities. Upon hovering on one of the cities, I would like all 4 lines going to/from that city to become highlighted. The hover option inside mapview had no effect, when I tried it. Thanks.

library(dplyr)
library(readr)
library(janitor)
library(sp)
library(purrr)

cc = read_csv("http://techslides.com/demos/country-capitals.csv")

nodes = 
  cc %>% 
  clean_names() %>% 
  mutate(capital_latitude = as.numeric(capital_latitude)) %>% 
  select(capital_name, capital_longitude, capital_latitude) %>% 
  filter(capital_name %in% c("Warsaw", "El-Aaiún", "Jamestown", "Antananarivo", "Manama"))

edges = 
  full_join(
  nodes %>% rename(from = capital_name, from_lon = capital_longitude, from_lat = capital_latitude) %>% mutate(index = 1),
  nodes %>% rename(to = capital_name, to_lon = capital_longitude, to_lat = capital_latitude) %>% mutate(index = 1),
  by = "index") %>% 
  mutate(from_to = paste(from, "_", to)) %>% 
  filter(from != to) %>% 
  select(-index) %>% 
  rowwise() %>%  
  mutate(capital_lines = pmap(list(from_lon = from_lon, from_lat = from_lat, to_lon = to_lon, to_lat = to_lat, from_to = from_to),  
                             function(from_lon, from_lat, to_lon, to_lat, from_to) {
                               Line(cbind(c(from_lon, to_lon), 
                                          c(from_lat, to_lat))) %>% 
                                 Lines(., ID = from_to)}
  )) %>% 
  mutate(capital_lines = list(SpatialLines(list(capital_lines))))


mapview(nodes, xcol = "capital_longitude", ycol = "capital_latitude") +
  mapview(do.call(rbind, edges$capital_lines))
1

There are 1 best solutions below

0
On
library(mapview)
mapviewOptions(fgb = FALSE)
mapview(shp, highlight = leaflet::highlightOptions(color = "red", weight = 2, sendToBack = TRUE))

This works for me. See details in https://github.com/r-spatial/mapview/issues/392.

enter image description here