{
let container = html`<div style='height:600px;' />`;
yield container;
const client = tgmCoreClient("westcentraleurope");
let map = new mapboxgl.Map({
container,
center: [13.3694, 52.5251],
zoom: 13,
minZoom: 11,
style: client.basemaps.getGLStyleURL("Basic"),
scrollZoom: false,
attributionControl: false
})
.addControl(new mapboxgl.NavigationControl())
.addControl(
new mapboxgl.AttributionControl({
compact: true,
customAttribution: attribution
})
);
const getBbox = () => {
const { _sw: southWest, _ne: northEast } = map.getBounds();
return { southWest, northEast };
};
const getPlaces = async (bounds) => {
const places = await client.pois.boundingBox(bounds, {
group: ["public_transport_station", "bus_stop", "tram_stop"]
});
const placesGeoJSON = turf.featureCollection(
places.map((p) => {
const { groupIds, tags } = p;
const group = groupIds.includes("bus_stop")
? "bus"
: groupIds.includes("tram_stop")
? "rail-light"
: tags.ferry === "yes"
? "ferry"
: tags.subway === "yes"
? "rail-metro"
: "rail";
return turf.point([p.lng, p.lat], {
tags,
group: group
});
})
);
return placesGeoJSON;
};
map.on("load", async () => {
for (let icon of [
"tgm-bus",
"tgm-rail",
"tgm-rail-light",
"tgm-rail-metro",
"tgm-ferry",
"tgm-square"
]) {
map.loadImage(
`https://releases.targomo.com/tgm-icons/images/${icon}.png`,
(error, image) => {
if (error) throw error;
if (!map.hasImage(icon)) map.addImage(icon, image, { sdf: true });
}
);
}
map.addSource("pois", {
type: "geojson",
data: turf.featureCollection([]),
cluster: true,
clusterMaxZoom: 16,
clusterRadius: 50
});
map.addLayer({
id: "single-badge",
type: "symbol",
source: "pois",
filter: ["all", ["!", ["has", "point_count"]]],
layout: {
"icon-image": "tgm-square",
"icon-size": 0.25,
"icon-allow-overlap": true
},
paint: {
"icon-color": "#2871cc"
}
});
map.addLayer({
id: "single",
type: "symbol",
source: "pois",
filter: ["all", ["!", ["has", "point_count"]]],
layout: {
"icon-image": "tgm-{group}",
"icon-size": 0.15,
"icon-allow-overlap": true
},
paint: {
"icon-color": "#fff"
}
});
map.addLayer({
id: "multi",
type: "symbol",
source: "pois",
filter: ["has", "point_count"],
layout: {
"text-field": ["get", "point_count"],
"text-size": 8,
"text-allow-overlap": true,
"text-font": ["Open Sans Bold"],
"icon-image": "tgm-square",
"icon-size": 0.25
},
paint: {
"icon-color": "#003f9a",
"text-color": "#fff"
}
});
const POIs = await getPlaces(getBbox());
map.getSource("pois").setData(POIs);
});
map.on("moveend", async () => {
const POIs = await getPlaces(getBbox());
map.getSource("pois").setData(POIs);
});
}