Skip to contents

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.

Usage

markerShape(
  path,
  width = 100,
  height = 100,
  icon = c(x = width/2, y = height/2, size = min(width, height)/2),
  anchor = c(x = width/2, y = height/2),
  popup = anchor,
  shadow = c("drop", "cast", "none")
)

Arguments

path

The shape to draw. Either the d attribute 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"/>'). Use NULL for 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 x and y coordinates of its centre and its size (the side of the square the icon is fitted into). Unnamed vectors are taken to be in the order x, 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 x and y. 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 x and y. Defaults to anchor.

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 from anchor up and to the right, as if the shape were standing upright on its tip, like the default leaflet::addMarkers() marker. Suits pointed shapes.

  • "none": no shadow.

Value

A leafmagic_marker_shape object.

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.

See also

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