Markers and popups
A marker is a pin at a position. A popup is a bubble of content pointing at a place; Google Maps calls it an InfoWindow. Click the blue markers, and drag the red one:
Live: this is the code below, running.
const map = new milsat.Map(document.getElementById("map"), {
apiKey: "YOUR_API_KEY",
center: [3.3792, 6.5244],
zoom: 14,
});
const shops = [
{ name: "Head office", position: [3.3792, 6.5244] },
{ name: "Warehouse", position: [3.3850, 6.5290] },
{ name: "Pickup point", position: [3.3735, 6.5205] },
];
for (const shop of shops) {
const marker = new milsat.Marker({
position: shop.position,
map,
title: shop.name,
color: "#2563eb",
popup: new milsat.Popup({ text: shop.name }),
});
marker.on("click", () => console.log("Clicked", shop.name));
}
// A marker the user can drag.
const pin = new milsat.Marker({ position: [3.3820, 6.5220], map, draggable: true, color: "#dc2626" });
pin.on("dragend", (event) => {
document.getElementById("output").textContent =
`Dropped at ${event.position.lat.toFixed(5)}, ${event.position.lng.toFixed(5)}`;
});Marker
| Option | |
|---|---|
position | Required. |
map | Put it on a map now. Or later with marker.setMap(map); setMap(null) takes it off. |
title | Shown on hover, and read by screen readers. |
color | Colour of the pin. |
element | Your own element instead of the pin. |
draggable | Let people move it. Listen for dragend. |
popup | Opens when the marker is clicked. |
Methods: setPosition, getPosition, setMap, setPopup, setDraggable, getElement, remove. Events: click, dragend.
Popup
Give it one of text, html or element. Use text for anything that comes from your users: it is shown as written. html is not cleaned, so only use it for content you control.
const popup = new milsat.Popup({ text: "Gate 2 is closed on Sundays" });
popup.open(map, [3.3792, 6.5244]); // on its own
popup.close();
// Built from your own elements
const card = document.createElement("div");
card.textContent = order.customerName; // safe: textContent never runs HTML
new milsat.Popup({ element: card }).open(map, order.position);Options: position, closeButton (default true), closeOnClick (default true), maxWidth (default "260px"). Events: open, close.