Creates a marker shape for use with the marker argument of magicIcons().
A shape is drawn using SVG in its own coordinate system, width units wide
and height units tall. The icon is then placed in a square within the
shape, and the whole marker is scaled so that it is markerSize pixels wide
on the map. All of the built-in shapes ("marker", "circle", etc.) are
defined in this way.
Arguments
- path
The shape to draw. Either the
dattribute of an SVG<path>(e.g.,"M50 2 98 98 2 98z"for a triangle), or SVG markup of one or more elements (e.g.,'<circle cx="50" cy="50" r="48"/>'). UseNULLfor no shape, in which case only the icon is drawn.- width, height
The size of the coordinate system the shape is drawn in. Their ratio defines the aspect ratio of the marker.
- icon
Where to draw the icon, as a numeric vector of the
xandycoordinates of its centre and itssize(the side of the square the icon is fitted into). Unnamed vectors are taken to be in the orderx,y,size.- anchor
The coordinates of the point of the shape which sits on the marker's location on the map, as a numeric vector of
xandy. Defaults to the centre of the shape. For pointed shapes, such as"marker", this should be the tip.- popup
The coordinates from which popups open, as a numeric vector of
xandy. Defaults toanchor.- shadow
The style of shadow drawn beneath the marker when
magicIcons(shadow = TRUE). One of:"drop"(the default): a soft shadow just below the shape, as if it were lying flat on the map. Suits shapes centred on their location."cast": a shadow cast fromanchorup and to the right, as if the shape were standing upright on its tip, like the defaultleaflet::addMarkers()marker. Suits pointed shapes."none": no shadow.
Drawing shapes
The shape is filled with markerColor and outlined
in a slightly darker colour with a stroke width of 2 units, so path
should not usually set its own fill or stroke. As the outline is
centred on the edge of the shape, leave a margin of at least 1 unit
between the shape and the edges of the coordinate system so that it isn't
clipped.
Shadows
As in {leaflet}, shadows are drawn as separate images in
the map's shadow pane, so a marker's shadow never covers another marker.
Shadows are generated from the shape itself, so work for any shape. Shapes
with no path never have a shadow.
Examples
# a downwards-pointing triangle, anchored at its tip
arrow <- markerShape(
"M2 2h96L50 98z",
icon = c(x = 50, y = 34, size = 36),
anchor = c(x = 50, y = 98),
popup = c(x = 50, y = 10),
shadow = "cast"
)
arrow
#> <markerShape> 100 x 100
#> * icon: size 36 centred at (50, 34)
#> * anchor: (50, 98)
#> * popup: (50, 10)
#> * shadow: cast
# use it with magicIcons()
library(leaflet)
leaflet(port_talbot) |>
addTiles() |>
addMarkers(
lng = ~longitude,
lat = ~latitude,
icon = magicIcons("industry", marker = arrow)
)
