{
map.on("load", () => {
map.addSource("subwayStationSource", {
type: "geojson",
data: subwayGeoJson.station,
cluster: true,
clusterMaxZoom: 9,
clusterRadius: 50
});
map.addSource("subwayLineSource", {
type: "geojson",
lineMetrics: true,
data: subwayGeoJson.line
});
map.addLayer({
type: "line",
source: "subwayLineSource",
id: "subwayLine",
paint: {
"line-color": ["get", "cl"],
"line-width": 2
},
layout: {
"line-cap": "round",
"line-join": "round"
}
});
map.addLayer({
id: "clusters",
type: "circle",
source: "subwayStationSource",
filter: ["has", "point_count"],
paint: {
// Use step expressions (https://docs.mapbox.com/mapbox-gl-js/style-spec/#expressions-step)
// with three steps to implement three types of circles:
// * Blue, 20px circles when point count is less than 100
// * Yellow, 30px circles when point count is between 100 and 750
// * Pink, 40px circles when point count is greater than or equal to 750
"circle-color": [
"step",
["get", "point_count"],
"#51bbd6",
5,
"#f1f075",
10,
"#f28cb1"
],
"circle-opacity": 0.5,
"circle-radius": ["step", ["get", "point_count"], 20, 100, 30, 750, 40]
}
});
map.addLayer({
id: "cluster-count",
type: "symbol",
source: "subwayStationSource",
filter: ["has", "point_count"],
layout: {
"text-field": "{point_count_abbreviated}",
"text-font": ["DIN Offc Pro Medium", "Arial Unicode MS Bold"],
"text-size": 12
},
paint: {
"text-opacity": 0.5
}
});
map.addLayer({
id: "station-symbol",
type: "symbol",
source: "subwayStationSource",
filter: ["!", ["has", "point_count"]],
layout: {
"text-field": ["get", "n"],
"text-font": ["DIN Offc Pro Medium", "Arial Unicode MS Bold"],
"text-size": 12,
"text-offset": [0, 1]
},
paint: {
"text-color": "#aaaaaa"
}
});
map.addLayer({
id: "unclustered-point",
type: "circle",
source: "subwayStationSource",
filter: ["!", ["has", "point_count"]],
paint: {
"circle-color": ["get", "cl"],
"circle-radius": ["*", ["get", "nLoad"], 0.05], //4,
"circle-opacity": 0.5,
"circle-stroke-width": 1,
"circle-stroke-color": "#fff"
}
});
// inspect a cluster on click
map.on("click", "clusters", (e) => {
const features = map.queryRenderedFeatures(e.point, {
layers: ["clusters"]
});
const clusterId = features[0].properties.cluster_id;
map
.getSource("subwayStationSource")
.getClusterExpansionZoom(clusterId, (err, zoom) => {
if (err) return;
map.easeTo({
center: features[0].geometry.coordinates,
zoom: zoom
});
});
});
// When a click event occurs on a feature in
// the unclustered-point layer, open a popup at
// the location of the feature, with
// description HTML from its properties.
map.on("click", "unclustered-point", (e) => {
const coordinates = e.features[0].geometry.coordinates.slice();
const mag = e.features[0].properties.mag;
const tsunami = e.features[0].properties.tsunami === 1 ? "yes" : "no";
// Ensure that if the map is zoomed out such that
// multiple copies of the feature are visible, the
// popup appears over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
new mapboxgl.Popup()
.setLngLat(coordinates)
.setHTML(`magnitude: ${mag}<br>Was there a tsunami?: ${tsunami}`)
.addTo(map);
});
map.on("mouseenter", "clusters", () => {
map.getCanvas().style.cursor = "pointer";
});
map.on("mouseleave", "clusters", () => {
map.getCanvas().style.cursor = "";
});
// Add handlers
{
map.on("dragend", () => {
console.log("dragend");
updateMapboxParamDiv();
});
map.on("zoomend", () => {
console.log("zoomend");
updateMapboxParamDiv();
});
map.on("pitchend", () => {
console.log("pitchend");
updateMapboxParamDiv();
});
}
});
}