Last active
March 1, 2023 14:17
-
-
Save malwoodsantoro/d7e2730e82dae141d80b04050452d4aa to your computer and use it in GitHub Desktop.
Cluster symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset='utf-8' /> | |
<title></title> | |
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' /> | |
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.40.1/mapbox-gl.js'></script> | |
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.40.1/mapbox-gl.css' rel='stylesheet' /> | |
<style> | |
body { margin:0; padding:0; } | |
#map { position:absolute; top:0; bottom:0; width:100%; } | |
</style> | |
</head> | |
<body> | |
<div id='map'></div> | |
<script> | |
mapboxgl.accessToken = 'pk.eyJ1IjoibWFsLXdvb2QiLCJhIjoiY2oyZ2t2em50MDAyMzJ3cnltMDFhb2NzdiJ9.X-D4Wvo5E5QxeP7K_I3O8w'; | |
var map = new mapboxgl.Map({ | |
container: 'map', | |
style: 'mapbox://styles/mapbox/dark-v9', | |
center: [-103.59179687498357, 40.66995747013945], | |
zoom: 3 | |
}); | |
map.on('load', function() { | |
map.addSource("baseball-parks", { | |
type: "geojson", | |
data: "./baseball-parks.geojson", | |
cluster: true, | |
clusterMaxZoom: 14, | |
clusterRadius: 50 | |
}); | |
map.addLayer({ | |
id: "clusters", | |
type: "symbol", | |
source: "baseball-parks", | |
filter: ["has", "point_count"], | |
layout: { | |
"icon-image": "stadium-15", | |
"text-field": "{point_count_abbreviated}", | |
"text-font": ["DIN Offc Pro Medium", "Arial Unicode MS Bold"], | |
"text-size": 12, | |
"text-anchor": "top", | |
"text-line-height": 2.2 | |
}, | |
paint: { | |
"text-color": "#8fc8f6" | |
} | |
}); | |
map.addLayer({ | |
id: "unclustered-point", | |
type: "circle", | |
source: "baseball-parks", | |
filter: ["!has", "point_count"], | |
paint: { | |
"circle-color": "#8fc8f6", | |
"circle-radius": 10, | |
"circle-stroke-color": "#fff" | |
} | |
}); | |
map.on('click', 'unclustered-point', function (e) { | |
console.log(e) | |
var coordinates = e.features[0].geometry.coordinates.slice(); | |
var ballpark = e.features[0].properties.Ballpark; | |
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) { | |
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360; | |
} | |
new mapboxgl.Popup() | |
.setLngLat(coordinates) | |
.setHTML(ballpark) | |
.addTo(map); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment