-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps.html
277 lines (240 loc) · 8.63 KB
/
maps.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>TownSuite Maps</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.52.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.52.0/mapbox-gl.css' rel='stylesheet' />
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v2.3.0/mapbox-gl-geocoder.min.js'></script>
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v2.3.0/mapbox-gl-geocoder.css' type='text/css' />
<script src='https://api.tiles.mapbox.com/mapbox.js/plugins/turf/v3.0.11/turf.min.js'></script>
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.0.9/mapbox-gl-draw.js'></script>
<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.0.9/mapbox-gl-draw.css' type='text/css'/>
<script src='/assets/js/maps.js'></script>
<link href="/assets/css/maps.css" type="text/css" crossorigin="anonymous" rel="stylesheet" />
</head>
<body>
<div id="close"><p id="words"><a class="close" href="#" onclick="toggle_visibility('box');">X</a></p></div>
<div id="wrapper">
<div id="box">
<img src="../assets/logo.svg" alt="" src="/" href="#" class="logo" />
<div id='geocoder' class='geocoder'></div>
<p id="words">Select Layers</p>
<nav id="layers"></nav>
<p id="words">Map Style</p>
<div id="menu"> <input id='basic' type='radio' name='rtoggle' value='basic'>
<label for='basic'>basic</label>
<input id='bright' type='radio' name='rtoggle' value='bright'>
<label for='bright'>bright</label>
<input id='light' type='radio' name='rtoggle' value='light'>
<label for='light'>light</label>
<input id='dark' type='radio' name='rtoggle' value='dark' checked='checked'>
<label for='dark'>dark</label>
<input id='satellite' type='radio' name='rtoggle' value='satellite'>
<label for='satellite'>satellite</label></div>
<br>
</div>
<div id="map"></div>
<div class='calculation-box'>
<div id='calculated-area'></div>
</div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoidG93bnN1aXRlIiwiYSI6ImNpeWpzazB4ZjAwMDIzMnBpMzE4bGpqa3UifQ.orkjN3Z4d9kur1A4pNSnig';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/dark-v9',
zoom: 15,
center: [-54.6052, 48.9553]
});
var draw = new MapboxDraw({
displayControlsDefault: false,
controls: {
polygon: true,
trash: true
}
});
map.addControl(draw);
map.on('draw.create', updateArea);
map.on('draw.delete', updateArea);
map.on('draw.update', updateArea);
function updateArea(e) {
var data = draw.getAll();
var answer = document.getElementById('calculated-area');
if (data.features.length > 0) {
var area = turf.area(data);
// restrict to area to 2 decimal points
var rounded_area = Math.round(area*100)/100;
answer.innerHTML = '<p><strong>' + rounded_area + '</strong></p><p>square meters</p>';
} else {
answer.innerHTML = '';
if (e.type !== 'draw.delete') alert("Use the draw tools to draw a polygon!");
}
};
var marker = new mapboxgl.Marker({
draggable: true
})
.setLngLat([-54.6052, 48.9553])
.addTo(map);
function onDragEnd() {
var lngLat = marker.getLngLat();
coordinates.style.display = 'block';
coordinates.innerHTML = 'Longitude: ' + lngLat.lng + '<br />Latitude: ' + lngLat.lat;
}
marker.on('dragend', onDragEnd);
var geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken
});
document.getElementById('geocoder').appendChild(geocoder.onAdd(map));
map.on('load', function () {
map.addSource('contours', {
type: 'vector',
url: 'mapbox://mapbox.mapbox-terrain-v2'
});
map.addLayer({
'id': 'contours',
'type': 'line',
'source': 'contours',
'source-layer': 'contour',
'layout': {
'visibility': 'none',
'line-join': 'round',
'line-cap': 'round'
},
'paint': {
'line-color': '#877b59',
'line-width': 1
}
});
map.addSource('lights', {
type: 'vector',
url: 'mapbox://townsuite.cjrcpsngq221y2wn52usl72i4-39tne'
});
map.addLayer({
'id': 'lights',
'type': 'circle',
'source': 'lights',
'layout': {
'visibility': 'none'
},
'paint': {
'circle-radius': 8,
'circle-color': 'rgba(148,55,23,1)'
},
'source-layer': 'Test_points_2'
});
map.addSource('bob', {
type: 'vector',
url: 'mapbox://townsuite.cjrca3xm502jg2wpfo2wv8vh2-4k1z2'
});
map.addLayer({
'id': 'bob',
'type': 'circle',
'source': 'bob',
'layout': {
'visibility': 'none'
},
'paint': {
'circle-radius': 8,
'circle-color': 'rgba(55,148,179,1)'
},
'source-layer': 'Test_points'
});
});
var toggleableLayerIds = [ 'contours','lights', 'bob' ];
for (var i = 0; i < toggleableLayerIds.length; i++) {
var id = toggleableLayerIds[i];
var link = document.createElement('a');
link.href = '#';
link.className = 'hover';
link.textContent = id;
link.onclick = function (e) {
var clickedLayer = this.textContent;
e.preventDefault();
e.stopPropagation();
var visibility = map.getLayoutProperty(clickedLayer, 'visibility');
if (visibility === 'visible') {
map.setLayoutProperty(clickedLayer, 'visibility', 'none');
this.className = '';
} else {
this.className = 'active';
map.setLayoutProperty(clickedLayer, 'visibility', 'visible');
}
};
var layers = document.getElementById('layers');
layers.appendChild(link);
}
map.on('load', function () {
// Add a layer showing the places.
map.addLayer({
"id": "places",
"type": "symbol",
"source": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"description": "<strong>TownSuite Municipal Software</strong><p><a href=\"https://townsuite.com\" target=\"_blank\" title=\"Opens in a new window\">TownSuite</a> is software company that focuses on creating municipal apps.</p>",
"icon": "star"
},
"geometry": {
"type": "Point",
"coordinates": [-54.6052, 48.9553]
}
}, {
"type": "Feature",
"properties": {
"description":"Hello world",
"icon": "star"
},
"geometry": {
"type": "Point",
"coordinates": [-54.5552, 48.9553]
}
}]
}
},
"layout": {
"icon-image": "{icon}-15",
"icon-allow-overlap": true
}
});
// When a click event occurs on a feature in the places layer, open a popup at the
// location of the feature, with description HTML from its properties.
map.on('click', 'places', function (e) {
var coordinates = e.features[0].geometry.coordinates.slice();
var description = e.features[0].properties.description;
// 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(description)
.addTo(map);
});
// Change the cursor to a pointer when the mouse is over the places layer.
map.on('mouseenter', 'places', function () {
map.getCanvas().style.cursor = 'pointer';
});
// Change it back to a pointer when it leaves.
map.on('mouseleave', 'places', function () {
map.getCanvas().style.cursor = '';
});
});
var layerList = document.getElementById('menu');
var inputs = layerList.getElementsByTagName('input')
function switchLayer(layer) {
var layerId = layer.target.id;
map.setStyle('mapbox://styles/mapbox/' + layerId + '-v9');
};
for (var i = 0; i < inputs.length; i++) {
inputs[i].onclick = switchLayer;
};
</script>
</body>
</html>