Marker HTML CSS

Create markers with HTML and CSS

  1. Example
  2. Running the Sample Locally

Example

Marker HTML CSS
        interface MarkerData {
  price: string;
}

function initMap(): void {
  class HTMLMapMarker extends woosmap.map.OverlayView {
    private readonly latlng: woosmap.map.LatLng;
    private readonly html: string;
    public div: HTMLDivElement | null = null;

    constructor({ latlng, map, data }) {
      super();
      this.latlng = latlng;
      this.html = `<div class="popover-content">
                      <span class="icon">️</span>
                      <span class="price">${data.price}</span>
                    </div>
                     <div class="popover-arrow-shadow"></div>
                     <div class="popover-arrow"></div>`;
      this.setMap(map);
    }

    private createDiv(): void {
      this.div = document.createElement("div");
      this.div.style.position = "absolute";
      this.div.className = "popover";
      if (this.html) {
        this.div.innerHTML = this.html;
      }
      woosmap.map.event.addDomListener(this.div, "click", () => {
        woosmap.map.event.trigger(this, "click");
      });
    }

    private appendDivToOverlay(): void {
      const panes = this.getPanes();
      if (panes && this.div) {
        panes.floatPane.appendChild(this.div);
      }
    }

    private positionDiv(): void {
      const point = this.getProjection()?.fromLatLngToDivPixel(this.latlng);
      if (point && this.div) {
        // Offset should depend on the style of your popover
        const offsetWidth = this.div.offsetWidth / 2; // 50% of div width
        const offsetHeight = this.div.offsetHeight + 6; // Full height of div plus the arrow height
        // @ts-ignore
        this.div.style.left = `${point.x - offsetWidth}px`;
        // @ts-ignore
        this.div.style.top = `${point.y - offsetHeight}px`;
      }
    }

    draw(): void {
      this.positionDiv();
    }

    onAdd(): void {
      if (!this.div) {
        this.createDiv();
        this.appendDivToOverlay();
      }
    }

    onRemove(): void {
      if (this.div && this.div.parentNode) {
        this.div.parentNode.removeChild(this.div);
        this.div = null;
      }
    }

    getPosition(): woosmap.map.LatLng {
      return this.latlng;
    }

    addEventListener(eventName: string, callback: () => void): void {
      if (this.div) {
        this.div.addEventListener(eventName, callback);
      }
    }
  }

  const center: woosmap.map.LatLngLiteral = { lat: 51.5074, lng: -0.1278 };

  const map = new woosmap.map.Map(
    document.getElementById("map") as HTMLElement,
    {
      zoom: 13,
      center: center,
    },
  );

  const markersData: MarkerData[] = [
    { price: "100 €" },
    { price: "200 €" },
    { price: "300 €" },
    { price: "400 €" },
    { price: "500 €" },
  ];

  const positions: woosmap.map.LatLngLiteral[] = [
    { lat: 51.5074, lng: -0.1278 },
    { lat: 51.5174, lng: -0.1378 },
    { lat: 51.4974, lng: -0.1178 },
    { lat: 51.5074, lng: -0.1478 },
    { lat: 51.5074, lng: -0.1078 },
  ];

  const markers: HTMLMapMarker[] = [];

  markersData.forEach((data, index) => {
    const marker = new HTMLMapMarker({
      latlng: new woosmap.map.LatLng(
        positions[index].lat,
        positions[index].lng,
      ),
      map: map,
      data: data,
    });

    marker.addEventListener("click", () => {
      markers.forEach((m) => {
        if (m.div) {
          m.div.classList.remove("active");
        }
      });
      if (marker.div) {
        marker.div.classList.add("active");
      }
    });

    markers.push(marker);
  });
}

declare global {
  interface Window {
    initMap: () => void;
  }
}
window.initMap = initMap;

    
            constructor({ latlng, map, data }) {
      super();
      this.latlng = latlng;
      this.html = `<div class="popover-content">
                      <span class="icon">️</span>
                      <span class="price">${data.price}</span>
                    </div>
                     <div class="popover-arrow-shadow"></div>
                     <div class="popover-arrow"></div>`;
      this.setMap(map);
    }
    createDiv() {
      this.div = document.createElement("div");
      this.div.style.position = "absolute";
      this.div.className = "popover";
      if (this.html) {
        this.div.innerHTML = this.html;
      }

      woosmap.map.event.addDomListener(this.div, "click", () => {
        woosmap.map.event.trigger(this, "click");
      });
    }
    appendDivToOverlay() {
      const panes = this.getPanes();

      if (panes && this.div) {
        panes.floatPane.appendChild(this.div);
      }
    }
    positionDiv() {
      const point = this.getProjection()?.fromLatLngToDivPixel(this.latlng);

      if (point && this.div) {
        // Offset should depend on the style of your popover
        const offsetWidth = this.div.offsetWidth / 2; // 50% of div width
        const offsetHeight = this.div.offsetHeight + 6; // Full height of div plus the arrow height

        // @ts-ignore
        this.div.style.left = `${point.x - offsetWidth}px`;
        // @ts-ignore
        this.div.style.top = `${point.y - offsetHeight}px`;
      }
    }
    draw() {
      this.positionDiv();
    }
    onAdd() {
      if (!this.div) {
        this.createDiv();
        this.appendDivToOverlay();
      }
    }
    onRemove() {
      if (this.div && this.div.parentNode) {
        this.div.parentNode.removeChild(this.div);
        this.div = null;
      }
    }
    getPosition() {
      return this.latlng;
    }
    addEventListener(eventName, callback) {
      if (this.div) {
        this.div.addEventListener(eventName, callback);
      }
    }
  }

  const center = { lat: 51.5074, lng: -0.1278 };
  const map = new woosmap.map.Map(document.getElementById("map"), {
    zoom: 13,
    center: center,
  });
  const markersData = [
    { price: "100 €" },
    { price: "200 €" },
    { price: "300 €" },
    { price: "400 €" },
    { price: "500 €" },
  ];
  const positions = [
    { lat: 51.5074, lng: -0.1278 },
    { lat: 51.5174, lng: -0.1378 },
    { lat: 51.4974, lng: -0.1178 },
    { lat: 51.5074, lng: -0.1478 },
    { lat: 51.5074, lng: -0.1078 },
  ];
  const markers = [];

  markersData.forEach((data, index) => {
    const marker = new HTMLMapMarker({
      latlng: new woosmap.map.LatLng(
        positions[index].lat,
        positions[index].lng,
      ),
      map: map,
      data: data,
    });

    marker.addEventListener("click", () => {
      markers.forEach((m) => {
        if (m.div) {
          m.div.classList.remove("active");
        }
      });
      if (marker.div) {
        marker.div.classList.add("active");
      }
    });
    markers.push(marker);
  });
}

window.initMap = initMap;

    
        /*
 * Always set the map height explicitly to define the size of the div element
 * that contains the map.
 */
#map {
  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;
}

.popover {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  cursor: pointer;
}
.popover .popover-content {
  display: flex;
  align-items: center;
  background-color: #FFF;
  color: #1D1D1D;
  padding: 6px 8px;
  border-radius: 20px;
  font-family: Roboto, Arial, sans-serif;
  font-size: 15px;
  font-weight: 500;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  z-index: 1;
}
.popover .popover-content .icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background-color: #f50057;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  margin-right: 6px;
  background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMjRweCIgdmlld0JveD0iMCAtOTYwIDk2MCA5NjAiIHdpZHRoPSIyNHB4IiBmaWxsPSIjRkZGRkZGIj48cGF0aCBkPSJNNDAtMjAwdi02MDBoODB2NDAwaDMyMHYtMzIwaDMyMHE2NiAwIDExMyA0N3Q0NyAxMTN2MzYwaC04MHYtMTIwSDEyMHYxMjBINDBabTI0MC0yNDBxLTUwIDAtODUtMzV0LTM1LTg1cTAtNTAgMzUtODV0ODUtMzVxNTAgMCA4NSAzNXQzNSA4NXEwIDUwLTM1IDg1dC04NSAzNVoiLz48L3N2Zz4=");
  background-repeat: no-repeat;
  background-size: 16px 16px;
  background-position: center;
}
.popover .popover-content .price {
  white-space: nowrap;
}
.popover .popover-arrow-shadow {
  position: absolute;
  bottom: -7px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-top: 7px solid rgba(0, 0, 0, 0.2);
  filter: blur(2px);
  z-index: 0;
}
.popover .popover-arrow {
  position: absolute;
  bottom: -6px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 6px solid #FFF;
  z-index: 1;
}
.popover.active .popover-content {
  background-color: #f50057;
  color: #FFFFFF;
}
.popover.active .popover-content .icon {
  background-color: #FFFFFF;
  background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMjRweCIgdmlld0JveD0iMCAtOTYwIDk2MCA5NjAiIHdpZHRoPSIyNHB4IiBmaWxsPSIjZjUwMDU3Ij48cGF0aCBkPSJNNDAtMjAwdi02MDBoODB2NDAwaDMyMHYtMzIwaDMyMHE2NiAwIDExMyA0N3Q0NyAxMTN2MzYwaC04MHYtMTIwSDEyMHYxMjBINDBabTI0MC0yNDBxLTUwIDAtODUtMzV0LTM1LTg1cTAtNTAgMzUtODV0ODUtMzVxNTAgMCA4NSAzNXQzNSA4NXEwIDUwLTM1IDg1dC04NSAzNVoiLz48L3N2Zz4=");
}
.popover.active .popover-arrow {
  border-top: 6px solid #f50057;
}


    
        <html>
  <head>
    <title>Marker HTML CSS</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>
    <div id="map"></div>

    <script
      src="https://sdk.woosmap.com/map/map.js?key=woos-48c80350-88aa-333e-835a-07f4b658a9a4&callback=initMap"
      defer
    ></script>
  </body>
</html>

    

Running the Sample Locally

Before you can run this sample on your local machine, you need to have Git and Node.js installed. If they’re not already installed, follow these instructions to get them set up.

Once you have Git and Node.js installed, you can run the sample by following these steps:

  1. Clone the repository and navigate to the directory of the sample.

  2. Install the necessary dependencies.

  3. Start running the sample.

Here are the commands you can use in your terminal to do this:

Shell
        git clone -b sample/marker-html-css https://github.com/woosmap/js-samples.git
cd js-samples
npm i
npm start

    

You can experiment with other samples by switching to any branch that starts with the pattern sample/SAMPLE_NAME.

Shell
        git checkout sample/SAMPLE_NAME
npm i
npm start

    
Was this article helpful?
Have more questions? Submit a request