Adding custom markers

Hello.

I've been using the leaflet package for creating geo-spatial data lately and I've been trying the examples in the leaflet for R website (https://rstudio.github.io/leaflet/markers.html).

Currently, my objective is to add a logo as a custom marker to the map. While I could add the markers given in the examples, I'm unable to add other images in the internet as a possible marker for my map. Why's that so ?

Could you share the code you're trying to use to add the markers? It helps us help you to see what you're working with.

For example, below I've changed the example from the link you gave to use a little picture of an angler fish (note that I've changed the dimensions of the image accordingly, but haven't changed anything else, so you still have the shadow image from the greenLeafIcon example)

library(leaflet)

data(quakes)
leaflet(data = quakes[1:20,]) %>% addTiles() %>%
  addMarkers(~long, ~lat, popup = ~as.character(mag), label = ~as.character(mag))


anglerIcon <- makeIcon(
  iconUrl = "https://i.imgur.com/otS5q0K.png",
  iconWidth = 64, iconHeight = 64,
  iconAnchorX = 22, iconAnchorY = 94,
  shadowUrl = "http://leafletjs.com/examples/custom-icons/leaf-shadow.png",
  shadowWidth = 50, shadowHeight = 64,
  shadowAnchorX = 4, shadowAnchorY = 62
)

leaflet(data = quakes[1:4,]) %>% addTiles() %>%
  addMarkers(~long, ~lat, icon = anglerIcon)

Created on 2019-06-18 by the reprex package (v0.3.0)

3 Likes

Hey Mara. The code worked once I pasted the link of the icons from the imgur website. Thank you !
I initially tried to paste the URL of the images from google images but to no avail.

Here's the code, that's finally working.

newleaf <- makeIcon(
iconUrl = "https://i.imgur.com/kiydjCM.png",
iconWidth = 80, iconHeight = 60,
iconAnchorX = 22, iconAnchorY = 94,
shadowUrl = "http://leafletjs.com/examples/custom-icons/leaf-shadow.png",
shadowWidth = 50, shadowHeight = 95,
shadowAnchorX = 2, shadowAnchorY = 100
)

leaflet(data = quakes[1:20,]) %>% #Subsets 20 rows of the quakes dataset
addTiles() %>% #Adds the default tile for the map
addMarkers(~long, ~lat, icon = newleaf)

The output:

Oh, and that's a Crustle with a Dugtrio, which symbolically represents the boulders and the earthquakes in the Pokemon series. All credits to the imgur website. Thank you !

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.