Change color of leaflet marker

后端 未结 5 1282
甜味超标
甜味超标 2020-12-05 08:15

Is there anyway to change the color of leaflet marker base on the value of some variable. In the following map, for example, I wish to assign marker color based on mag varia

5条回答
  •  囚心锁ツ
    2020-12-05 08:34

    As far as I know, you need to assign an image file to one level of icon. For instance, if you have three levels in magnitude in the earthquake data, you need to create an icon list with three image paths. Then, you can have three different colors in markers. At least, the following example is getting closer to what you want. I edited a png file and created three png files. You need to specify the paths of the file when you make an icon list.

    library(dplyr)
    library(leaflet)
    
    mutate(quakes, group = cut(mag, breaks = c(0, 5, 6, Inf), labels = c("blue", "green", "orange"))) -> mydf
    
    ### I edit this png file and created my own marker.
    ### https://raw.githubusercontent.com/lvoogdt/Leaflet.awesome-markers/master/dist/images/markers-soft.png
    quakeIcons <- iconList(blue = makeIcon("/Users/jazzurro/Documents/Stack Overflow/blue.png", iconWidth = 24, iconHeight =32),
                           green = makeIcon("/Users/jazzurro/Documents/Stack Overflow/green.png", iconWidth = 24, iconHeight =32),
                           orange = makeIcon("/Users/jazzurro/Documents/Stack Overflow/orange.png", iconWidth = 24, iconHeight =32))
    
    
    leaflet(data = mydf[1:100,]) %>% 
    addTiles() %>%
    addMarkers(icon = ~quakeIcons[group])
    

提交回复
热议问题