Marker HTML CSS

Sample code for Woosmap Map JavaScript API author(s): Woosmap

HTML

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

    <!-- jsFiddle will insert css and js -->
  </head>
  <body>
    <div id="map"></div>

    <script
      src="https://sdk.woosmap.com/map/map.js?key=woos-e1e5490b-19d1-34f8-9955-fd5cfc8c880f&callback=initMap"
      defer
    ></script>
  </body>
</html>

CSS

/*
 * 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);
 ...

JavaScript

function initMap() {
  class HTMLMapMarker extends woosmap.map.OverlayView {
    latlng;
    html;
    div = 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);
    }
    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) {
   ...