-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
407 lines (371 loc) · 13 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Find an address</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src='https://api.mapbox.com/mapbox-gl-js/v2.0.1/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v2.0.1/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
#geocoder-container > div {
min-width:50%;
margin-left:25%;
}
#menu {
position: absolute;
background: #fff;
padding: 10px;
font-family: 'Open Sans', sans-serif;
max-width: 200px;
}
#info {
display: block;
position: relative;
margin: 0px auto;
width: 50%;
padding: 10px;
border: none;
border-radius: 3px;
font-size: 12px;
text-align: center;
color: #222;
background: #fff;
}
.mapboxgl-popup {
max-width: 400px;
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
padding-bottom: 10px;
}
.mapboxgl-popup, img {
max-width: 100%;
}
.mapboxgl-popup-content {
border-radius: 10px;
color: #ffffff;
background: rgba(0, 30, 84, 0.78);
}
.mapboxgl-popup-content a {
color: #ffffff;
line-height: 2em;
}
.mapboxgl-popup-content h2, p {
margin-bottom: 5px;
}
.mapboxgl-popup-anchor-bottom .mapboxgl-popup-tip {
border-top-color: rgba(0, 30, 84, 0.78);
}
.mapboxgl-popup-anchor-top .mapboxgl-popup-tip {
border-bottom-color: rgba(0, 30, 84, 0.78);
}
.mapboxgl-popup-anchor-left .mapboxgl-popup-tip {
border-right-color: rgba(0, 30, 84, 0.78);
}
.mapboxgl-popup-anchor-right .mapboxgl-popup-tip {
border-left-color: rgba(0, 30, 84, 0.78);
}
.mapboxgl-popup-anchor-top-left .mapboxgl-popup-tip {
border-bottom-color: rgba(0, 30, 84, 0.78);
}
.mapboxgl-popup-anchor-top-right .mapboxgl-popup-tip {
border-bottom-color: rgba(0, 30, 84, 0.78);
}
.mapboxgl-popup-anchor-bottom-left .mapboxgl-popup-tip {
border-top-color: rgba(0, 30, 84, 0.78);
}
.mapboxgl-popup-anchor-bottom-right .mapboxgl-popup-tip {
border-top-color: rgba(0, 30, 84, 0.78);
}
</style>
</head>
<body>
<script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.5.1/mapbox-gl-geocoder.min.js"></script>
<link
rel="stylesheet"
href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.5.1/mapbox-gl-geocoder.css"
type="text/css"
/>
<!-- Promise polyfill script required to use Mapbox GL Geocoder in IE 11 -->
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js"></script>
<div id="map"></div>
<pre id="info"></pre>
<div id="menu">
<input id="ckjixv8hq03d019qp0ddf44wn" type="radio" name="rtoggle" value="streets" checked="checked">
<label for="streets">Mapbox Satellite Streets</label><br>
<input id="ckeae0gbx03wg19qdx6w2wcgx" type="radio" name="rtoggle" value="streets">
<label for="streets">Mapbox Streets</label><br>
<input id="cjcw7qyt316bv2rqy4z88mnii" type="radio" name="rtoggle" value="Le Shine">
<label for="Le Shine">Le Shine</label><br>
<input id="ckeepi9oj03rm1aoykj4ni5io" type="radio" name="rtoggle" value="Runner">
<label for="Runner">Runner</label><br>
<input id="cj4a864yh11cg2sqtexzm9qc0" type="radio" name="rtoggle" value="North Star">
<label for="North Star">North Star</label><br>
<input id="cj8osqo2z91oz2rpps52rxe1v" type="radio" name="rtoggle" value="Scenic">
<label for="Scenic">Scenic</label><br>
<input id="cja1so8j804gt2spohx34fpzz" type="radio" name="rtoggle" value="Odyssey">
<label for="Odyssey">Odyssey</label><br>
<input id="cj6lscjta8em92slk4zraoirj" type="radio" name="rtoggle" value="Standard">
<label for="Standard">Standard</label><br>
<input id="cj686227613282sns30deqrlq" type="radio" name="rtoggle" value="Terminal">
<label for="Terminal">Terminal</label><br>
<input id="cj1v81h91000d2sql97n8vhdp" type="radio" name="rtoggle" value="Colourlovers">
<label for="Colourlovers">Colourlovers</label><br>
<input id="ckjixv8hq03d019qp0ddf44wn" type="radio" name="rtoggle" value="satellite">
<label for="satellite">Satellite</label><br>
</div>
<script>
mapboxgl.accessToken = "pk.eyJ1IjoiZ3JhZmEiLCJhIjoiY2pka3k3anpwMDBycTM0cWg3YnpiOW1yZSJ9.msQZ437kHXUN4p09ywU6rg";
var map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/grafa/ckjixv8hq03d019qp0ddf44wn",
center: [-96, 37.8],
zoom: 3
});
/* given a query in the form "lng, lat" or "lat, lng" returns the matching
* geographic coordinate(s) as search results in carmen geojson format,
* https://github.com/mapbox/carmen/blob/master/carmen-geojson.md
*/
var coordinatesGeocoder = function (query) {
// match anything which looks like a decimal degrees coordinate pair
var matches = query.match(/^[ ]*(?:Lat: )?(-?\d+\.?\d*)[, ]+(?:Lng: )?(-?\d+\.?\d*)[ ]*$/i);
if (!matches) {
return null;
}
function coordinateFeature(lng, lat) {
return {
center: [lng, lat],
geometry: {
type: "Point",
coordinates: [lng, lat]
},
place_name: "Lat: " + lat + " Lng: " + lng, // eslint-disable-line camelcase
place_type: ["coordinate"], // eslint-disable-line camelcase
properties: {},
type: "Feature",
};
}
var coord1 = Number(matches[1]);
var coord2 = Number(matches[2]);
var geocodes = [];
if (coord1 < -90 || coord1 > 90) {
// must be lng, lat
geocodes.push(coordinateFeature(coord1, coord2));
}
if (coord2 < -90 || coord2 > 90) {
// must be lat, lng
geocodes.push(coordinateFeature(coord2, coord1));
}
if (geocodes.length === 0) {
// else could be either lng, lat or lat, lng
geocodes.push(coordinateFeature(coord1, coord2));
geocodes.push(coordinateFeature(coord2, coord1));
}
return geocodes;
};
var geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
localGeocoder: coordinatesGeocoder,
zoom: 14,
placeholder: "Try: -40, 170",
mapboxgl: mapboxgl,
marker: {
color: "rgba(255, 153, 0, 0.85)"
}
});
map.addControl(geocoder);
let gcPopup = new mapboxgl.Popup({
// offset: popupOffsets
});
let destPopup = new mapboxgl.Popup({
// offset: popupOffsets
});
geocoder.on('result', geoPopup)
function geoPopup() {
let addyEncoded = encodeURI(geocoder.inputString);
let popupCoords = geocoder.mapMarker.getLngLat().toArray();
geocoder.mapMarker.setLngLat(popupCoords)
.setPopup(gcPopup.setHTML("<h3><a href='geo://" + popupCoords[1] + "," + popupCoords[0] + "'>Start Navigation in 1Tap</a></h3>" +
"<h3><a href='https://straightaway.app.link/demo/nav?address=" + addyEncoded + "'>Start Navigation in Straightaway</a></h3>" +
'🚗 <a href="javascript:void(0)" onclick="getLocation(getRoute)";return false;>View route</a>'));
geocoder.mapMarker.addTo(map);
}
// Add geolocate control to the map.
map.addControl(
new mapboxgl.GeolocateControl({
positionOptions: {
enableHighAccuracy: true
},
trackUserLocation: true
})
);
// geolocation
const getLocation = (callback) => {
navigator.geolocation.getCurrentPosition(function(position) {
let startLon = position.coords.longitude;
let startLat = position.coords.latitude;
callback(startLon,startLat);
});
};
// do directions stuff
// directions
const getRoute = (startLon,startLat,dest) => {
console.log(dest);
var dest = gcPopup.getLngLat();
let url = 'https://api.mapbox.com/directions/v5/mapbox/driving/' + startLon + ',' + startLat + ';' + dest.lng + ',' + dest.lat + '?geometries=geojson&overview=full&access_token=' + mapboxgl.accessToken;
let req = new XMLHttpRequest();
req.responseType = 'json';
req.open('GET', url, true);
req.onload = function() {
let data = req.response.routes[0];
let route = data.geometry.coordinates;
let geojsonRoute = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": route
}
};
let geojsonStart = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [startLon,startLat]
}
};
if (map.getSource('route')) {
map.getSource('route').setData(geojsonRoute);
map.getSource('start').setData(geojsonStart);
} else {
map.addLayer({
"id": "start",
"type": "symbol",
"source": {
"type": "geojson",
"data": geojsonStart
},
"layout": {
"icon-image": "circle-stroked-15",
"text-field": "{title}",
"text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
"text-offset": [0, 0.6],
"text-anchor": "top"
}
},'waterway-label');
map.addLayer({
"id": "route",
"type": "line",
"source": {
"type": "geojson",
"data": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": route
}
}
},
"layout": {
"line-join": "round",
"line-cap": "round"
},
"paint": {
"line-color": "rgba(0, 30, 84, 0.78)",
"line-width": 10,
"line-opacity": 0.75
}
},'waterway-label');
map.addLayer({
id: 'routearrows',
type: 'symbol',
source: 'route',
layout: {
'symbol-placement': 'line',
'text-field': '▶',
'text-size': {
base: 1,
stops: [[12, 18], [22, 60]]
},
'symbol-spacing': {
base: 1,
stops: [[12, 30], [22, 160]]
},
'text-keep-upright': false
},
paint: {
'text-color': '#fff',
'text-halo-color': 'hsl(55, 11%, 96%)',
// 'text-halo-width': 3
}
}, 'waterway-label');
};
let coordinates = geojsonRoute.geometry.coordinates;
// Pass the first coordinates in the LineString to `lngLatBounds` &
// wrap each coordinate pair in `extend` to include them in the bounds
// result. A letiation of this technique could be applied to zooming
// to the bounds of multiple Points or Polygon geomteries - it just
// requires wrapping all the coordinates with the extend method.
let bounds = coordinates.reduce(function(bounds, coord) {
return bounds.extend(coord);
}, new mapboxgl.LngLatBounds(coordinates[0], coordinates[0]));
map.fitBounds(bounds, {
padding: 20
});
destPopup.setLngLat([startLon,startLat])
.setHTML('<span class="duration">' + Math.floor(data.duration/60) + ' min 🚗💨</span>');
destPopup.addTo(map);
};
req.send();
};
// layer manager
var layerList = document.getElementById("menu");
var inputs = layerList.getElementsByTagName("input");
function switchLayer(layer) {
var layerId = layer.target.id;
map.setStyle("mapbox://styles/grafa/" + layerId);
}
for (var i = 0; i < inputs.length; i++) {
inputs[i].onclick = switchLayer;
}
map.on("load", function() {
// Listen for the `geocoder.input` event that is triggered when a user
// makes a selection and add a symbol that matches the result.
geocoder.on("result", function(ev) {
c = ev.result.geometry.coordinates;
document.getElementById("info").innerHTML =
"<a href='geo:" + c[1] + "," + c[0] + "'>"+ ev.result.geometry.coordinates + "</a><br />" +
ev.result.place_name + " | <a href='https://www.openstreetmap.org/#map=19/" + c[1] + "/" + c[0] +"'>OSM</a>";
console.log(ev.result.geometry.coordinates);
});
map.addSource('mapbox-dem', {
'type': 'raster-dem',
'url': 'mapbox://mapbox.mapbox-terrain-dem-v1',
'tileSize': 512,
'maxzoom': 14
});
// add the DEM source as a terrain layer with exaggerated height
map.setTerrain({ 'source': 'mapbox-dem', 'exaggeration': 2.0 });
// add a sky layer that will show when the map is highly pitched
map.addLayer({
'id': 'sky',
'type': 'sky',
'paint': {
'sky-type': 'atmosphere',
'sky-atmosphere-sun': [0.0, 0.0],
'sky-atmosphere-sun-intensity': 15
}
});
// console.log(JSON.stringify(polyline.decode(line2,5).map(c => [c[1],c[0]])));
if (window.location.hash) {
var search = decodeURIComponent(window.location.hash.split('#search=')[1]);
geocoder.setInput(search);
geocoder.query(search);
}
});
</script>
</body>
</html>