-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
95 lines (90 loc) · 3.14 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<script src="https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.js"></script>
<link
href="https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.css"
rel="stylesheet"
/>
<script src="https://cdn.what3words.com/javascript-components@4-latest/dist/what3words/what3words.js?key=29IK5VZP"></script>
<title>Grid Section Map Overlay (Mapbox)</title>
</head>
<body>
body {margin: 0;padding: 0;}
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
<script>
// Create the Mapbox
mapboxgl.accessToken =
"pk.eyJ1IjoiZjFrdW5pIiwiYSI6ImNsaTl3Mmg5bDI2Z3ozcG53NTFnYzkyOHcifQ.kO1fY4a4TraHhQxoLYwTEg";
let map = new mapboxgl.Map({
container: "map", // container id
style: "mapbox://styles/mapbox/streets-v9", // stylesheet location
center: [139.74349, 35.65447], // starting position [lng, lat]
zoom: 18 // starting zoom
});
map.addControl(new mapboxgl.NavigationControl());
</script>
<script>
// On map load and move events, invoke the drawGrid function
map.on("load", drawGrid).on("move", drawGrid);
// Check to see if the requisit zoom level has been reached, and if so, draw the grid onto the map
function drawGrid() {
const zoom = map.getZoom();
const loadFeatures = zoom > 11;
if (loadFeatures) {
// Zoom level is high enough
var ne = map.getBounds().getNorthEast();
var sw = map.getBounds().getSouthWest();
// Call the what3words Grid API to obtain the grid squares within the current visble bounding box
what3words.api
.gridSectionGeoJson({
southwest: {
lat: sw.lat,
lng: sw.lng
},
northeast: {
lat: ne.lat,
lng: ne.lng
}
})
.then(function (data) {
var grid = map.getSource("grid");
if (grid === undefined) {
map.addSource("grid", {
type: "geojson",
data: data
});
map.addLayer({
id: "grid_layer",
type: "line",
source: "grid",
layout: {
"line-join": "round",
"line-cap": "round"
},
paint: {
"line-color": "#777",
"line-width": 0.5
}
});
} else {
map.getSource("grid").setData(data);
}
})
.catch(console.error);
}
var grid_layer = map.getLayer("grid_layer");
if (typeof grid_layer !== "undefined") {
map.setLayoutProperty(
"grid_layer",
"visibility",
loadFeatures ? "visible" : "none"
);
}
}
</script>
</body>
</html>