Change Color of Leaflet Marker

Change color of leaflet marker

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])

Sample Image

Leaflet map: on click changing color of all markers

When adding markers push them into an array. const markers = [] somewhere at the beginning and than markers.push(L.marker(... Than you can manipulate all by iterating over the array or single one if you add some kind of unique key to your points. Current one you can access with this too.

I edited the fiddle, please check the link.

R Shiny with Leaflet - change color of marker after click

We can use addAwesomeMarkers to customize the icon color as suggested in the docs and use leafletProxy to change it on click:

library(shiny)
library(sf)
library(leaflet)
library(geojsonsf)

getData <- function(){
poly <- '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[7.207031249999999,46.97463048970666],[7.18231201171875,46.89867745059795],[7.267456054687499,46.86864162233212],[7.392425537109376,46.85831292242506],[7.529754638671874,46.86864162233212],[7.678070068359375,46.9061837801476],[7.683563232421874,46.97556750833867],[7.592926025390624,47.03082254778662],[7.371826171874999,47.01584377790821],[7.207031249999999,46.97463048970666]]]}}]}'

sf_poly <- geojson_sf(poly)
points <- st_as_sf(st_sample(sf_poly, 20))
points$id <- 1:nrow(points)
coords <- st_coordinates(points)

df <- data.frame(st_drop_geometry(points), coords)
return(df)
}

ui <- fluidPage(
titlePanel("Leaflet Map"),
sidebarLayout(
sidebarPanel(
textInput(inputId="selected_photos", label="Selected images", value = "", placeholder = NULL)
),
mainPanel(
leafletOutput("mymap")
)
)
)

server <- function(input, output, session) {
#https://groups.google.com/g/shiny-discuss/c/LWk4ZYNhsSc
points <- getData()
points$clicked <- FALSE
RV <- reactiveValues(points = points)

icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'white',
library = 'ion',
markerColor = "blue"
)

output$mymap <- renderLeaflet({
leaflet() %>%
#addTiles() %>%
addProviderTiles("OpenStreetMap", group = "OSM") %>%
addAwesomeMarkers(data = points, lng = ~X, lat = ~Y, layerId = ~id, icon = icons)
})

myLeafletProxy <- leafletProxy(mapId = "mymap", session)

observeEvent(input$mymap_marker_click,{
clicked_point <- input$mymap_marker_click
RV$points[points$id==clicked_point$id,]$clicked <- !(RV$points[points$id==clicked_point$id,]$clicked)

updateTextInput(inputId = "selected_photos", value = paste(unlist(RV$points$id[which(RV$points$clicked)]), collapse = ", "))

removeMarker(map = myLeafletProxy, layerId = clicked_point$id)
addAwesomeMarkers(map = myLeafletProxy,
lng = clicked_point$lng,
lat = clicked_point$lat,
layerId = clicked_point$id,
icon = awesomeIcons(
icon = 'ios-close',
iconColor = 'white',
library = 'ion',
markerColor = ifelse(RV$points[clicked_point$id,]$clicked, yes = "red", no = "blue")
))
})
}

shinyApp(ui, server)

result



Related Topics



Leave a reply



Submit