Popup when hover with leaflet in R?

前端 未结 2 476
小鲜肉
小鲜肉 2021-01-31 19:18

My leaflet map looks something like this:

library(sp)
library(leaflet)
circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
  r = diameter / 2
         


        
2条回答
  •  北海茫月
    2021-01-31 20:04

    Here is an alternative:

    library(leaflet)
    library(htmltools)
    library(htmlwidgets)
    
    yourmap <- leaflet(height = "400px") %>% 
      addTiles() %>%
      addPolygons(data = Circle.Town, color = 'green',  fillOpacity = .7) %>%
      addCircleMarkers(data = df1, lat = ~lat, lng =~long, 
                       radius = ~VAM, popup = ~as.character(X), 
                       fillColor = ~pal(VAM),
                       stroke = FALSE, fillOpacity = 0.8,
                       clusterOptions = markerClusterOptions()) %>% 
      addLegend(position = "topright",
                pal = pal, values = df1$VAM,
                title = "VAM",
                opacity = 1
      ) %>% 
      setView(lng = 1, lat = -1, zoom = 8)
    
    setwd("~/Desktop/")
    saveWidget(yourmap, file="yourmap.html")
    

    In your desktop, you will have an html and a folder saved under yourmap. Open the leaflet.js file located in /pathTo/yourmap_files/leaflet-binding-1.0.1.9002. In leaflet.js, scroll down to var popup = df.get(i, 'popup'); and paste just below:

              marker.on('mouseover', function (e) {
        this.openPopup();
    });
    marker.on('mouseout', function (e) {
        this.closePopup();
    });
    

    Save and reopen yourmap.html file. Hover on one of your point!!

提交回复
热议问题