-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpoly-features.html
424 lines (407 loc) · 15.8 KB
/
poly-features.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
<!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 routes/paths or regions — Maps for HTML reference examples</title>
<link rel="stylesheet" href="examples.css">
<script src="api-keys.js"></script>
<script src="data/glooston.js"></script>
</head>
<body>
<h1>Examples for
<a href="../#use-case-poly-features">Use Case: Display routes/paths or regions</a>
</h1>
<p>
These examples show the boundary of the parish of Glooston, Leicestershire, UK.
The boundary is defined as a GeoJSON object in the file
<a href="./data/glooston.js">/examples/data/glooston.js</a>.
This was exported from OpenStreetMap data.
</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>
{
const map = L.map('leaflet-js-map').setView([52.5548, -0.8971], 13);
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);
const style = {
color: 'rgb(60,59,255)',
fillColor: 'rgba(60,59,255,0.8)',
weight: 1,
opacity: 1
};
const glooston = L.geoJSON(m4h.geojson.glooston, {style: style});
glooston.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 vectorSource = new ol.source.Vector({
features: new ol.format.GeoJSON().readFeatures(m4h.geojson.glooston, {
featureProjection: 'EPSG:3857'
})
});
const polygonStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color: [60,59,255],
width: 1
}),
fill: new ol.style.Fill({
color: [60,59,255,0.6],
})
});
const vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: polygonStyle
});
const map = new ol.Map({
view: new ol.View({
center: ol.proj.fromLonLat([-0.8971, 52.5548]),
zoom: 13
}),
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vectorLayer
],
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: 52.5548,
lng: -0.8971
},
zoom: 13
}
);
map.data.addGeoJson(m4h.geojson.glooston);
map.data.setStyle({
fillColor: 'rgba(60,59,255,0.8)',
strokeColor: 'rgb(60,59,255)',
strokeWeight: 1
});
}
</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(52.5548, -0.8971),
zoom: 13
});
Microsoft.Maps.loadModule('Microsoft.Maps.GeoJson', () => {
const glooston = Microsoft.Maps.GeoJson.read(m4h.geojson.glooston, {
polygonOptions: {
fillColor: 'rgba(60,59,255,0.5)',
strokeColor: 'rgb(60,59,255)',
strokeThickness: 1
}
});
map.entities.push(glooston);
});
}
</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(52.5548, -0.8971),
span = new mapkit.CoordinateSpan(0.03, 0.03),
region = new mapkit.CoordinateRegion(center, span);
const map = new mapkit.Map(document.getElementById('mapkit-js-map'), {
region: region
});
mapkit.importGeoJSON(m4h.geojson.glooston, {
styleForOverlay: (overlay, geoJSON) => {
return new mapkit.Style({
strokeColor: 'rgb(60,59,255)',
strokeOpacity: 1,
fillColor: 'rgb(60,59,255)',
fillOpacity: 0.6,
lineWidth: 1
});
},
geoJSONDidComplete: (result, geoJSON) => {
console.log('geoJSONDidComplete', result);
map.addOverlay(result.items[0]);
}
});
}
</script>
</code>
</section>
<section id="mapbox-api">
<h2>Mapbox GL JS API</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 = [-0.8971, 52.5548];
const map = new mapboxgl.Map({
container: 'mapbox-api-map',
style: 'mapbox://styles/mapbox/streets-v9',
center: mapCenter,
zoom: 12
});
map.on('load', () => {
map.addLayer({
id: 'glooston',
type: 'fill',
source: {
type: 'geojson',
data: m4h.geojson.glooston
},
paint: {
'fill-color': 'rgb(60,59,255)',
'fill-opacity': 0.8,
'fill-outline-color': 'rgb(40,39,101)'
}
});
});
}
</script>
</code>
</section>
<section id="tomtom">
<h2>TomTom Maps SDK for Web with vector maps</h2>
<div id="tomtom-single-location" 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-single-location', {
key: m4h.keys.tomtom,
source: 'vector',
basePath: 'https://api.tomtom.com/maps-sdk-js/4.47.6/examples/sdk',
center: [52.5548, -0.8971],
zoom: 13
});
const style = {
color: 'rgb(60,59,255)',
fillColor: 'rgba(60,59,255,0.8)',
weight: 1,
opacity: 1
};
const glooston = tomtom.L.geoJSON(m4h.geojson.glooston, {style: style});
glooston.addTo(map);
}
</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('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);
const genericProperties = [
'landuse',
'waterway',
'highway',
'building',
'amenity',
'railway',
'leisure'
];
// load data and attach to map
d3.json('data/geojson/glooston-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 = [];
Object.keys(feature.properties).forEach(property => {
if (genericProperties.includes(property)) {
classValues.push(property);
}
if (feature.properties[property] !== null) {
let value = `${property}--${feature.properties[property]}`;
classValues.push(value);
}
});
return classValues.join(' ');
});
pathSelection
.data(m4h.geojson.glooston.features)
.enter()
.append('path')
.attr('stroke-width', STROKE_WIDTH)
.attr('d', pathGenerator)
.attr('class', feature => {
return 'overlay parish';
});
});
}
</script>
</code>
</section>
</body>
</html>