forked from mapbox/mapbox-gl-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeaturestate.html
More file actions
105 lines (92 loc) · 3.16 KB
/
Copy pathfeaturestate.html
File metadata and controls
105 lines (92 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<!DOCTYPE html>
<html>
<head>
<title>Mapbox GL JS debug page</title>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link rel='stylesheet' href='../dist/mapbox-gl.css' />
<style>
body { margin: 0; padding: 0; }
html, body, #map { height: 100%; }
#controls { position: absolute; top: 0; left: 0; padding:10px;}
</style>
</head>
<body>
<div id='map'></div>
<div id='controls'>
<div>
<label>Projection:</label>
<select id="projName">
<option value="mercator" selected>Mercator</option>
<option value="albers">Albers USA</option>
<option value="equalEarth">Equal Earth</option>
<option value="equirectangular">Equirectangular</option>
<option value="globe">Globe</option>
<option value="lambertConformalConic">Lambert Conformal Conic</option>
<option value="naturalEarth">Natural Earth</option>
<option value="winkelTripel">Winkel Tripel</option>
</select>
<label><input id='terrain-checkbox' type='checkbox'> terrain</label>
</div>
</div>
<script src='../dist/mapbox-gl-dev.js'></script>
<script src='../debug/access_token_generated.js'></script>
<script>
var map = window.map = new mapboxgl.Map({
container: 'map',
zoom: 4,
center: [-96, 38],
// style: {version: 8, layers: [], sources: {}}
style: 'mapbox://styles/mapbox/streets-v11', // style URL
});
map.on('load', () => {
map.addSource('mapbox-dem', {
"type": "raster-dem",
"url": "mapbox://mapbox.mapbox-terrain-dem-v1",
"tileSize": 512,
"maxzoom": 14
});
map.addSource('counties', {
"type": "vector",
"url": "mapbox://mapbox.82pkq93d",
"promoteId": {"original": "COUNTY"}
});
map.addLayer({
"id": "counties",
"type": "fill",
"source": "counties",
"source-layer": "original",
"paint": {
"fill-outline-color": "black",
"fill-color": ["case", ["boolean", ["feature-state", "hover"], false], "red", "lightgrey"]
}
});
let selectedCounty = null;
function resetFeatureState() {
if (selectedCounty) {
map.setFeatureState({source: 'counties', sourceLayer: 'original', id: selectedCounty}, {hover: false});
selectedCounty = null;
}
}
map.on("mouseleave", "counties", () => {
resetFeatureState();
});
map.on("mousemove", "counties", (e) => {
const feature = e.features[0];
if (selectedCounty !== feature.id) {
resetFeatureState();
map.setFeatureState({source: 'counties', sourceLayer: 'original', id: feature.id}, {hover: true});
selectedCounty = feature.id;
}
});
});
document.getElementById('projName').addEventListener('change', (e) => {
const el = document.getElementById('projName');
map.setProjection(el.options[el.selectedIndex].value);
});
document.getElementById('terrain-checkbox').onclick = function() {
map.setTerrain(this.checked ? {"source": "mapbox-dem"} : null);
};
</script>
</body>
</html>