-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcreate-map.html
355 lines (338 loc) · 13.1 KB
/
create-map.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
<!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>Display an interactive map — 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-create-map">
Use Case: Display an interactive map
</a>
</h1>
<p>
The embedded map viewers here show their default view,
which is generally a map of the whole globe
although it may be personalized to the viewer's location.
</p>
<p>
The JS API maps are the simplest viewers supported by each framework.
Some require an explicit position and zoom level to display anything;
the examples use a full-globe map (zoom 0) centered on 0° latitude and longitude.
Some frameworks also require explicit map tile source;
the examples in those cases use OpenStreetMap tiles.
</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</h2>
<iframe width="600" height="450" allowfullscreen allow="fullscreen"
title="Google Maps"
src="https://www.google.com/maps/embed">
</iframe>
</section>
<section id="openstreetmap">
<h2>OpenStreetMap</h2>
<iframe width="600" height="450" scrolling="no"
title="OpenStreetMap"
src="https://www.openstreetmap.org/export/embed.html">
</iframe>
<div class="map-links">
<small>
<a href="https://www.openstreetmap.org/">View Larger Map</a>
</small>
</div>
</section>
<section id="bing-maps-embed">
<h2>Bing Maps</h2>
<div>
<iframe width="600" height="450" scrolling="no"
title="Bing Maps"
src="https://www.bing.com/maps/embed?w=600&h=450&cp=27.683528083787735~5.306411922086829&lvl=1&typ=d&sty=r&src=SHELL&FORM=MBEDV8">
</iframe>
<div class="map-links">
<small>
<a id="largeMapLink" target="_blank" href="https://www.bing.com/maps?cp=27.683528083787735~5.306411922086829&sty=r&lvl=1&FORM=MBEDLD">View Larger Map</a> |
<a id="dirMapLink" target="_blank" href="https://www.bing.com/maps/directions?cp=27.683528083787735~5.306411922086829&sty=r&lvl=1&rtp=~pos.27.683528083787735_5.306411922086829____&FORM=MBEDLD">Get Directions</a>
</small>
</div>
</div>
</section>
<section id="mapbox-embed">
<h2>MapBox Studio embed</h2>
<div>
<iframe width="600" height="450"
title="MapBox Studio"
src="https://api.mapbox.com/styles/v1/nchan0154/cjx9sxbqt08611cp94xvmkwu1.html?fresh=true&access_token=pk.eyJ1IjoibmNoYW4wMTU0IiwiYSI6ImNqeDlzd3BrNjAxcjAzeXFuMjdodGowbnMifQ.wmDpzNGfADuQOSn1dABh7A#0/0/0">
</iframe>
</div>
</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>
{
const map = L.map('leaflet-js-map').setView([0,0], 0);
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);
}
</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>
{
const map = new ol.Map({
view: new ol.View({
center: ol.proj.fromLonLat([0,0]),
zoom: 0
}),
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'openlayers-map'
});
}
</script>
</code>
</section>
<section id="google-maps-api">
<h2>Google Maps API</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() {
const map = new google.maps.Map(
document.getElementById('google-maps-api-map'),
{
center:{
lat: 0,
lng: 0
},
zoom: 0
}
);
}
</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() {
const map = new Microsoft.Maps.Map(document.getElementById('bing-maps-api-map'), {
center: new Microsoft.Maps.Location(0, 0),
zoom: 0
});
}
</script>
</code>
</section>
<section id="mapkit-js">
<h2>MapKit JS (Apple Maps) API</h2>
<div id="mapkit-js-map" style="width: 600px; height: 450px;"></div>
<script src="https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.js"></script>
<code class="script-example">
<script>
{
mapkit.addEventListener('error', function(err) {
console.log(err);
});
let mapkitKey = m4h.keys.mapkit.maps4html;
if (self.location.hostname === 'localhost') {
if (self.location.protocol === 'https:') {
mapkitKey = m4h.keys.mapkit.httpslocalhost8000;
} else {
mapkitKey = m4h.keys.mapkit.localhost8000;
}
}
mapkit.init({
authorizationCallback: function(done) {
done(mapkitKey);
},
language: "en"
});
const center = new mapkit.Coordinate(0, 0),
span = new mapkit.CoordinateSpan(360, 180),
region = new mapkit.CoordinateRegion(center, span);
const map = new mapkit.Map(document.getElementById('mapkit-js-map'), {
region: region
});
}
</script>
</code>
</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>
{
mapboxgl.accessToken = m4h.keys.mapboxGL;
// token must be configured for your website
const map = new mapboxgl.Map({
container: 'mapbox-api-map',
style: 'mapbox://styles/mapbox/streets-v9'
});
}
</script>
</code>
</section>
<section id="tomtom">
<h2>TomTom Maps SDK for Web with vector maps</h2>
<div id="tomtom-map" style="width: 600px; height: 450px;"></div>
<script src='https://api.tomtom.com/maps-sdk-js/4.47.6/examples/sdk/tomtom.min.js'></script>
<!--<link rel='stylesheet' href='https://api.tomtom.com/maps-sdk-js/4.47.6/examples/sdk/map.css'>-->
<link rel='stylesheet' href='tomtom/maps-extract.css'>
<link rel='stylesheet' href='https://api.tomtom.com/maps-sdk-js/4.47.6/examples/elements.css'>
<code class="script-example">
<script>
{
tomtom.setProductInfo('Maps4HTML Examples', '4.47.6');
const map = tomtom.map('tomtom-map', {
key: m4h.keys.tomtom,
// token must be configured for your website
source: 'vector',
basePath: 'https://api.tomtom.com/maps-sdk-js/4.47.6/examples/sdk'
// or self-host the JS + CSS
});
}
</script>
</code>
</section>
<section id="d3-geo">
<h2>D3 Geographies APIs</h2>
<div id="d3-geo-map" class="d3-geo-map" style="width: 600px; height: 450px;"></div>
<script src="https://d3js.org/d3.v5.js"></script>
<code class="script-example">
<script>
{
const handleZoom = () => {
pathGroup.attr("transform", d3.event.transform);
pathGroup.selectAll('path').attr('stroke-width', STROKE_WIDTH * (1/d3.event.transform.k));
};
const zoomButtonClickHandler = (amount) => {
return () => {
svg.transition().call(zoom.scaleBy, amount);
};
};
const createButton = (text) => {
const button = document.createElement('button');
button.type = 'button';
button.textContent = text;
return () => { return button };
};
const d3MapContainer = d3.select('#d3-geo-map'),
// create zooming
MIN_ZOOM = 1,
MAX_ZOOM = 2^20,
STROKE_WIDTH = .5,
ZOOM_EXTENT = [MIN_ZOOM, MAX_ZOOM],
ZOOM_IN_FACTOR = 2,
ZOOM_OUT_FACTOR = 1/ZOOM_IN_FACTOR,
zoom = d3.zoom()
.scaleExtent(ZOOM_EXTENT)
.on('zoom', handleZoom),
zoomIn = d3MapContainer
.append(createButton('+'))
.on('click', zoomButtonClickHandler(ZOOM_IN_FACTOR)),
zoomOut = d3MapContainer
.append(createButton('-'))
.on('click', zoomButtonClickHandler(ZOOM_OUT_FACTOR)),
// create map and controls
mapElementBounds = d3MapContainer.node().getBoundingClientRect(),
width = mapElementBounds.width,
height = mapElementBounds.height,
svg = d3MapContainer
.append('svg')
// .attr('width', "100%")
// .attr('height', "100%")
.attr('transform', 'translate(0,0) scale(1)'),
background = svg.append('rect').attr('class', 'd3-background'),
pathGroup = svg.call(zoom).append('g'),
pathSelection = pathGroup.selectAll('path'),
projection = d3.geoEquirectangular(),
pathGenerator = d3.geoPath(projection);
// load data and attach to map
d3.json('data/geojson/countries-50m-filtered.geojson').then(world => {
projection.fitSize([width, height], world);
pathSelection
.data(world.features)
.enter()
.append('path')
.attr('stroke-width', STROKE_WIDTH)
.attr('d', pathGenerator)
.attr('class', feature => {
let classValues = ['country'];
Object.keys(feature.properties).forEach(property => {
if (feature.properties[property] !== null) {
let value = `${property}--${feature.properties[property]}`;
classValues.push(value);
}
});
return classValues.join(' ');
});
});
}
</script>
</code>
</section>
</body>
</html>