Add Markers

Display markers to identify locations on Woosmap Map.

Markers identify locations on the map and can respond to user events such as clicks.

Add a Marker

Markers come from the marker library. Load it, then create a woosmap.map.marker.AdvancedMarkerElement with an AdvancedMarkerElementOptions object:

JavaScript
        await woosmap.map.importLibrary("marker");

    
  • position (required) is the latitude and longitude of the marker.
  • content (optional) is the HTML element drawn at the position. Without it, the marker shows a default pin. To draw a pin in your own colors, create a PinElement and pass its element.
  • map (optional) is the Map to display the marker on. You can also call the marker’s setMap() method later.
Add a simple marker
        async function initMap(): Promise<void> {
  await woosmap.map.importLibrary("maps");
  await woosmap.map.importLibrary("marker");

  const center: woosmap.map.LatLngLiteral = { lat: 51.52, lng: -0.13 };

  const map = (document.getElementById("map") as woosmap.map.MapElement)
    .innerMap;
  map.moveCamera({
    zoom: 13,
    center: center,
  });

  const { PinElement } = woosmap.map.marker;
  const pin = new PinElement({
    background: "#5588FF",
    borderColor: "#3D6FE0",
    glyphColor: "#FFFFFF",
  });

  const marker = new woosmap.map.marker.AdvancedMarkerElement({
    position: map.getCenter(),
    content: pin.element,
  });
  marker.setMap(map);
}

initMap();

    
        async function initMap() {
  await woosmap.map.importLibrary("maps");
  await woosmap.map.importLibrary("marker");

  const center = { lat: 51.52, lng: -0.13 };
  const map = document.getElementById("map").innerMap;

  map.moveCamera({
    zoom: 13,
    center: center,
  });

  const { PinElement } = woosmap.map.marker;
  const pin = new PinElement({
    background: "#5588FF",
    borderColor: "#3D6FE0",
    glyphColor: "#FFFFFF",
  });
  const marker = new woosmap.map.marker.AdvancedMarkerElement({
    position: map.getCenter(),
    content: pin.element,
  });

  marker.setMap(map);
}

initMap();

    
        /*
 * Always set the map height explicitly to define the size of the element
 * that contains the map. `display: block` is required for <wmp-map>/<gmp-map>,
 * which default to inline like any other custom element outside a flex/grid
 * container (no-op for a plain <div>).
 */
#map {
  display: block;
  width: 100%;
  height: 100%;
}

/*
 * Optional: Makes the sample page fill the window.
 */
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, Noto Sans, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji;
}


    
        <html>
  <head>
    <title>Marker Simple</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta charset="utf-8" />

    <link rel="stylesheet" type="text/css" href="./style.css" />
    <script type="module" src="./index.js"></script>
  </head>
  <body>
    <wmp-map id="map"></wmp-map>

    <script>
      (g=>{var p,w=window,d=document,l="importLibrary",q="__ib__",e="The Woosmap Maps JavaScript API",o=w.woosmap||(w.woosmap={}),m=o.map||(o.map={}),r=new Set(String(g.libraries||"").split(",").filter(Boolean)),s=new URLSearchParams,u=()=>p||(p=new Promise((f,n)=>{var a=d.createElement("script");s.set("libraries",[...r]+"");for(var k in g)k!="libraries"&&g[k]&&s.set(k,g[k]);s.set("callback","woosmap.map."+q);a.src="https://sdk.woosmap.com/map/map.js?"+s;m[q]=f;a.onerror=()=>n(Error(e+" could not load."));a.nonce=d.querySelector("script[nonce]")?.nonce||"";d.head.append(a)}));m[l]?console.warn(e+" only loads once. Ignoring:",g):m[l]=(f,...n)=>r.add(f)&&u().then(()=>m[l](f,...n))})({
        key: "woos-e1e5490b-19d1-34f8-9955-fd5cfc8c880f",
        libraries: "marker",
      });
    </script>
  </body>
</html>

    

content accepts any HTML element, so you can style markers with CSS. See the HTML/CSS marker sample.

The older woosmap.map.Marker class is still available.

Remove a marker

To remove a marker from the map, call its setMap() method with null:

JavaScript
        marker.setMap(null);

    

Marker Click Event

A marker fires click events only when you set gmpClickable: true. Then register a listener with addListener():

JavaScript
        const marker = new woosmap.map.marker.AdvancedMarkerElement({
  position: latlng,
  gmpClickable: true,
  map,
});
marker.addListener("click", () => {
  console.log(marker.position);
});

    

The following example displays the world’s megacities. Clicking a marker shows the city’s name and population, and changes the pin colors to highlight it.

Marker labels

To draw a label inside a pin, set the glyphText of its PinElement, and glyphColor for the text color. Set glyphSrc instead to draw an image.

JavaScript
        const { PinElement } = woosmap.map.marker;
const pin = new PinElement({
  background: "#f50057",
  glyphText: "1",
  glyphColor: "#FFFFFF",
});
new woosmap.map.marker.AdvancedMarkerElement({
  position: map.getCenter(),
  content: pin.element,
  map,
});

    

The following example displays labeled markers when the user clicks on the map:

Was this helpful?