Marker HTML CSS
Create markers with HTML and CSS
Example
Marker HTML CSS
interface MarkerData {
price: string;
}
async function initMap(): Promise<void> {
await woosmap.map.importLibrary("maps");
await woosmap.map.importLibrary("marker");
function createMarkerContent(data: MarkerData): HTMLDivElement {
const div = document.createElement("div");
div.className = "popover";
div.innerHTML = `<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>`;
return div;
}
const center: woosmap.map.LatLngLiteral = { lat: 51.5074, lng: -0.1278 };
const map = (document.getElementById("map") as woosmap.map.MapElement).innerMap;
map.moveCamera({
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: woosmap.map.marker.AdvancedMarkerElement[] = [];
markersData.forEach((data, index) => {
const content = createMarkerContent(data);
const marker = new woosmap.map.marker.AdvancedMarkerElement({
position: positions[index],
map: map,
content,
gmpClickable: true,
});
marker.addListener("click", () => {
markers.forEach((m) => {
(m.content as HTMLDivElement).classList.remove("active");
});
content.classList.add("active");
});
markers.push(marker);
});
}
initMap();
div.className = "popover";
div.innerHTML = `<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>`;
return div;
}
const center = { lat: 51.5074, lng: -0.1278 };
const map = document.getElementById("map").innerMap;
map.moveCamera({
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 content = createMarkerContent(data);
const marker = new woosmap.map.marker.AdvancedMarkerElement({
position: positions[index],
map: map,
content,
gmpClickable: true,
});
marker.addListener("click", () => {
markers.forEach((m) => {
m.content.classList.remove("active");
});
content.classList.add("active");
});
markers.push(marker);
});
}
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;
}
.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>
<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>
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:
-
Clone the repository and navigate to the directory of the sample.
-
Install the necessary dependencies.
-
Start running the sample.
Here are the commands you can use in your terminal to do this:
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.
git checkout sample/SAMPLE_NAME
npm i
npm start