Documentation menu
Example
Click a feature for a popup
Click one of your points and get a popup built from its properties, with a pointer cursor on hover so people know it is clickable. The popup content is an element filled with textContent, so a property holding markup is shown as text, never run.
ts
import { createMap, DataLayer } from "@mapmap/maps";
const map = createMap({
container: "map",
apiKey: "snk_...",
attributionPosition: "bottom-left",
center: [-1.6, 53.2],
zoom: 6,
});
await map.whenReady();
new DataLayer(map, {
data: {
type: "FeatureCollection",
features: [
{ type: "Feature", properties: { name: "London depot", open: "06:00 to 22:00" },
geometry: { type: "Point", coordinates: [-0.1278, 51.5074] } },
{ type: "Feature", properties: { name: "Birmingham yard", open: "24 hours" },
geometry: { type: "Point", coordinates: [-1.8904, 52.4862] } },
{ type: "Feature", properties: { name: "Leeds cross-dock", open: "05:00 to 21:00" },
geometry: { type: "Point", coordinates: [-1.5491, 53.8008] } },
],
},
style: { simple: { color: "#1a6bff", radius: 8 } },
// A string is set as HTML, so build an element from your data instead.
popup: (feature) => {
const el = document.createElement("div");
el.textContent = feature.properties?.name + ", open " + feature.properties?.open;
return el;
},
});Worth knowing
- The popup sits on the point's own coordinates rather than wherever inside the circle the click landed.
- popup may return a string or an element. A string is inserted as HTML, so return a string only for markup you wrote yourself; anything from your data goes in through textContent, as here.
- For points with clustering, popups and nearest-place answers already wired to MapMap search, PlacesLayer does all of this in one call.