-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathadd-custom-control.html
475 lines (437 loc) · 19.3 KB
/
add-custom-control.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Add Custom Control — Maps for HTML reference examples</title>
<link rel="stylesheet" href="examples.css">
<script src="api-keys.js"></script>
</head>
<body>
<h1>Examples for
<a href="../#use-case-add-custom-control">Use Case: Add a custom control to a map</a>
</h1>
<p>
These examples use a custom control which is updated to show the
latitude and longitude of the map centre when the map is moved.
The map is initialised with a marker at its central point. When the map
has been moved to another position, the "Mark" button will move the marker
to the new centre. Whenever the map has been moved such that the marker
is not at the centre, the "Return" button will re-centre the map on the marker.
</p>
<details>
<summary>Jump to section…</summary>
<ul class="toc">
<li>Iframe embeds: <ul>
<li><a href="#google-maps-embed">Google Maps embed</a></li>
<li><a href="#openstreetmap">OpenStreetMap embed</a></li>
<li><a href="#bing-maps-embed">Bing Maps embed</a></li>
<li><a href="#mapbox-embed">Mapbox Studio embed</a></li>
</ul></li>
<li>Client-side frameworks: <ul>
<li><a href="#leaflet-js">Leaflet.js API</a></li>
<li><a href="#openlayers">OpenLayers API</a></li>
<li><a href="#google-maps-api">Google Maps Platform API</a></li>
<li><a href="#bing-maps-api">Bing Maps Control API</a></li>
<li><a href="#mapkit-js">MapKit JS (Apple Maps) API</a></li>
<li><a href="#mapbox-api">Mapbox GL JS API</a></li>
<li><a href="#tomtom">TomTom Maps SDK for Web with vector maps</a></li>
<li><a href="#d3-geo">D3 Geographies APIs</a></li>
</ul></li>
</ul>
</details>
<section id="google-maps-embed">
<h2>Google Maps embed</h2>
<p>Not applicable</p>
</section>
<section id="openstreetmap">
<h2>OpenStreetMap embed</h2>
<p>Not applicable</p>
</section>
<section id="bing-maps-embed">
<h2>Bing Maps embed</h2>
<p>Not applicable</p>
</section>
<section id="mapbox-embed">
<h2>MapBox Studio embed</h2>
<p>Not applicable</p>
</section>
<section id="leaflet-js">
<h2>Leaflet.js (with OpenStreetMap tiles)</h2>
<div id="leaflet-js-map" style="width: 600px; height: 450px;"></div>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""/>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og=="
crossorigin=""></script>
<code class="script-example">
<script>
{ // block to restrict let and const scope to this example
// create the map
const map = L.map('leaflet-js-map').setView([46.233226, 6.055737], 15);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
minZoom: 0,
maxZoom: 20,
attribution: 'Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors'
}).addTo(map);
// set up the center marker
const centerMarker = L.marker(map.getCenter());
centerMarker.addTo(map);
// set up the custom control
const createButton = (text, onclick) => {
const button = document.createElement('button');
button.setAttribute('type', 'button');
button.appendChild(document.createTextNode(text));
L.DomEvent.on(button, 'click', onclick);
return button;
};
const markButton = createButton('Mark', (ev) => {
centerMarker.setLatLng(map.getCenter());
});
const returnButton = createButton('Return', (ev) => {
map.setView(centerMarker.getLatLng());
});
L.Control.LatLonControl = L.Control.extend({
onAdd: (map) => {
const latLonContainer = document.createElement('div');
latLonContainer.classList.add('lat-lng', 'custom-control', 'leafletjs');
latLonContainer.textContent = 'Center: ';
const lat = latLonContainer.appendChild(document.createElement('span'));
latLonContainer.appendChild(document.createTextNode(','));
const lng = latLonContainer.appendChild(document.createElement('span'));
latLonContainer.appendChild(markButton);
latLonContainer.appendChild(returnButton);
const updateLatLon = (ev) => {
const currentCenter = map.getCenter();
lat.textContent = currentCenter.lat.toFixed(6);
lng.textContent = currentCenter.lng.toFixed(6);
};
map.on('moveend', updateLatLon);
updateLatLon();
return latLonContainer;
},
onRemove: (map) => {
L.DomEvent.off(markButton, 'click', markButtonOnClick);
L.DomEvent.off(returnButton, 'click', returnButtonOnClick)
}
});
L.Control.latLonControl = function(opts) {
return new L.Control.LatLonControl(opts);
};
L.Control.latLonControl({ position: 'bottomleft' }).addTo(map);
}
</script>
</code>
</section>
<section id="openlayers">
<h2>OpenLayers with OpenStreetMap tiles</h2>
<div id="openlayers-map" style="width: 600px; height: 450px;"></div>
<link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
<code class="script-example">
<script>
{ // block to restrict let and const scope to this example
// set up the map
const vectorLayerFeatures = [];
const vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector({
features: vectorLayerFeatures
})
});
const view = new ol.View({
center: ol.proj.fromLonLat([6.055737, 46.233226]),
zoom: 15
});
const map = new ol.Map({
view: view,
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vectorLayer
],
target: 'openlayers-map'
});
// set up the center marker
const markerSVGData = "data:image/svg+xml,%3Csvg width='36px' height='36px' viewBox='-18 -18 36 36' xmlns='http://www.w3.org/2000/svg'%3E%3Ctitle%3Eround-marker%3C/title%3E%3Cg stroke-width='3' stroke='%236af'%3E%3Ccircle fill='%23fc2' fill-opacity='0.7' r='16'/%3E%3Ccircle r='1.5'/%3E%3C/g%3E%3C/svg%3E";
// simple SVG circle icon,
// the marker will be centered by default
let style = new ol.style.Style({
image: new ol.style.Icon({
opacity: 1,
scale: 0.5,
src: markerSVGData
})
});
const centerMarker = new ol.Feature({
geometry: new ol.geom.Point(view.getCenter())
});
centerMarker.setStyle(style);
vectorLayer.getSource().addFeature(centerMarker);
// set up the custom control
const createButton = (text, onclick) => {
const button = document.createElement('button');
button.setAttribute('type', 'button');
button.appendChild(document.createTextNode(text));
button.addEventListener('click', onclick);
return button;
};
const markButton = createButton('Mark', (ev) => {
centerMarker.getGeometry().setCoordinates(view.getCenter());
});
const returnButton = createButton('Return', (ev) => {
view.setCenter(centerMarker.getGeometry().getCoordinates());
});
const openLayersLatLngControl = new ol.control.Control({
element: (() => {
const latLonContainer = document.createElement('div');
latLonContainer.classList.add('lat-lng', 'custom-control', 'openlayers');
latLonContainer.textContent = 'Center: ';
const lat = latLonContainer.appendChild(document.createElement('span'));
latLonContainer.appendChild(document.createTextNode(','));
const lng = latLonContainer.appendChild(document.createElement('span'));
latLonContainer.appendChild(markButton);
latLonContainer.appendChild(returnButton);
const updateLatLon = (ev) => {
const currentCenter = ol.proj.toLonLat(view.getCenter());
lat.textContent = currentCenter[1].toFixed(6);
lng.textContent = currentCenter[0].toFixed(6);
};
map.on('moveend', updateLatLon);
updateLatLon();
return latLonContainer;
})()
});
map.addControl(openLayersLatLngControl);
}
</script>
</code>
</section>
<section id="google-maps-api">
<h2>Google Maps</h2>
<div id="google-maps-api-map" style="width: 600px; height: 450px;"></div>
<script>
const apiUrl = 'https://maps.googleapis.com/maps/api/js?key=' + m4h.keys.google + '&callback=initMap',
scriptElement = document.createElement('script');
scriptElement.setAttribute('src', apiUrl);
document.body.appendChild(scriptElement);
</script>
<code class="script-example">
<script>
function initMap() {
// set up the map
const mapCenter = new google.maps.LatLng(46.233226, 6.055737);
const map = new google.maps.Map(
document.getElementById('google-maps-api-map'),
{
center:mapCenter,
zoom: 15
}
);
// set up the center marker
const centerMarker = new google.maps.Marker({
map: map,
position: mapCenter
});
// set up the custom control
const createButton = (text, onclick) => {
const button = document.createElement('button');
button.setAttribute('type', 'button');
button.appendChild(document.createTextNode(text));
button.addEventListener('click', onclick);
return button;
};
const markButton = createButton('Mark', (ev) => {
centerMarker.setPosition(map.getCenter());
});
const returnButton = createButton('Return', (ev) => {
map.setCenter(centerMarker.getPosition());
});
function GoogleLatLonControl(latLonContainer, map) {
latLonContainer.classList.add('lat-lng', 'custom-control', 'googlemaps');
latLonContainer.textContent = 'Center: ';
const lat = latLonContainer.appendChild(document.createElement('span'));
latLonContainer.appendChild(document.createTextNode(','));
const lng = latLonContainer.appendChild(document.createElement('span'));
latLonContainer.appendChild(markButton);
latLonContainer.appendChild(returnButton);
const updateLatLon = (ev) => {
let currentCenter = map.getCenter();
lat.textContent = currentCenter.lat().toFixed(6);
lng.textContent = currentCenter.lng().toFixed(6);
};
map.addListener('center_changed', updateLatLon);
updateLatLon();
}
// add the custom control
const latLonContainer = document.createElement('div');
const latLonControl = new GoogleLatLonControl(latLonContainer, map);
latLonContainer.index = 1;
map.controls[google.maps.ControlPosition.BOTTOM_RIGHT].push(latLonContainer);
}
</script>
</code>
</section>
<section id="bing-maps-api">
<h2>Bing Maps Control API</h2>
<div id="bing-maps-api-map" style="width: 600px; height: 450px;"></div>
<script>
const bingApiUrl = 'https://www.bing.com/api/maps/mapcontrol?key=' + m4h.keys.bing + '&callback=initBingMap',
bingScriptElement = document.createElement('script');
bingScriptElement.setAttribute('src', bingApiUrl);
document.body.appendChild(bingScriptElement);
</script>
<code class="script-example">
<script>
function initBingMap() {
// set up the map
const mapCenter = new Microsoft.Maps.Location(46.233226, 6.055737);
const map = new Microsoft.Maps.Map(document.getElementById('bing-maps-api-map'), {
center: mapCenter,
zoom: 15
});
// set up the center marker
const centerMarker = new Microsoft.Maps.Pushpin(mapCenter);
map.entities.push(centerMarker);
// set up the custom control
const createButton = (text, onclick) => {
const button = document.createElement('button');
button.setAttribute('type', 'button');
button.appendChild(document.createTextNode(text));
button.addEventListener('click', onclick);
return button;
};
const markButton = createButton('Mark', (ev) => {
centerMarker.setLocation(map.getCenter());
});
const returnButton = createButton('Return', (ev) => {
map.setView({
center: centerMarker.getLocation()
});
});
function BingLatLonControl() {}
let lat, lng;
const updateLatLon = (ev) => {
let currentCenter = map.getCenter();
lat.textContent = currentCenter.latitude.toFixed(6);
lng.textContent = currentCenter.longitude.toFixed(6);
};
BingLatLonControl.prototype = new Microsoft.Maps.CustomOverlay({
beneathLabels: false
});
BingLatLonControl.prototype.onAdd = function() {
const latLonContainer = document.createElement('div');
latLonContainer.classList.add('lat-lng', 'custom-control', 'bing');
latLonContainer.textContent = 'Centre: ';
lat = latLonContainer.appendChild(document.createElement('span'));
latLonContainer.appendChild(document.createTextNode(','));
lng = latLonContainer.appendChild(document.createElement('span'));
latLonContainer.appendChild(markButton);
latLonContainer.appendChild(returnButton);
Microsoft.Maps.Events.addHandler(map, 'viewchangeend', updateLatLon);
this.setHtmlElement(latLonContainer);
};
BingLatLonControl.prototype.onLoad = function() {
updateLatLon();
};
BingLatLonControl.prototype.onRemove = function() {
Microsoft.Maps.Events.removeHandler(map, 'viewchangeend', updateLatLon);
};
// add the custom control
map.layers.insert(new BingLatLonControl());
}
</script>
</code>
</section>
<section id="mapkit-js">
<h2>MapKit JS (Apple Maps) API</h2>
<p>Not applicable.</p>
</section>
<section id="mapbox-api">
<h2>MapBox GL</h2>
<div id="mapbox-api-map" style="width: 600px; height: 450px;"></div>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.0.0/mapbox-gl.css' rel='stylesheet' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.0.0/mapbox-gl.js'></script>
<code class="script-example">
<script>
{ // block to restrict let and const scope to this example
// set up map
mapboxgl.accessToken = m4h.keys.mapboxGL;
const mapCenter = [6.055737, 46.233226];
const map = new mapboxgl.Map({
container: 'mapbox-api-map',
style: 'mapbox://styles/mapbox/streets-v9',
center: mapCenter,
zoom: 15
});
// set up the center marker
const centerMarker = new mapboxgl.Marker();
centerMarker.setLngLat(mapCenter);
centerMarker.addTo(map);
// set up the custom control
const createButton = (text, onclick) => {
const button = document.createElement('button');
button.setAttribute('type', 'button');
button.appendChild(document.createTextNode(text));
button.addEventListener('click', onclick);
return button;
};
const markButton = createButton('Mark', (ev) => {
centerMarker.setLngLat(map.getCenter());
});
const returnButton = createButton('Return', (ev) => {
map.setCenter(centerMarker.getLngLat());
});
let lat, lng;
const updateLatLon = (ev) => {
let currentCenter = map.getCenter();
lat.textContent = currentCenter.lat.toFixed(6);
lng.textContent = currentCenter.lng.toFixed(6);
};
const mapboxglLatLngControl = {
onAdd: (map) => {
const latLonContainer = document.createElement('div');
latLonContainer.classList.add('lat-lng', 'custom-control', 'mapboxgl-ctrl');
latLonContainer.classList.add('lat-lng');
latLonContainer.classList.add('custom-control');
latLonContainer.textContent = 'Centre: ';
lat = latLonContainer.appendChild(document.createElement('span'));
latLonContainer.appendChild(document.createTextNode(','));
lng = latLonContainer.appendChild(document.createElement('span'));
latLonContainer.appendChild(markButton);
latLonContainer.appendChild(returnButton);
map.on('moveend', updateLatLon);
updateLatLon();
return latLonContainer;
},
getDefaultPosition: () => {
return 'bottom-left'
},
onRemove: () => {
map.off('moveend', updateLatLon);
}
};
map.addControl(mapboxglLatLngControl);
}
</script>
</code>
</section>
<section id="tomtom">
<h2>TomTom Maps SDK for Web with vector maps</h2>
***TODO<!--
Replace with code including link/external scripts. Custom script has <code class="script-example"> parent element.
or <p>Not applicable</p>
-->***
</section>
<section id="d3-geo">
<h2>D3 Geographies APIs</h2>
***TODO<!--
Replace with code including link/external scripts. Custom script has <code class="script-example"> parent element.
or <p>Not applicable</p>
-->***
</section>
</body>
</html>