-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap-demo.html
192 lines (164 loc) · 7.07 KB
/
map-demo.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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Crowdsourced Data</title>
<link rel="shortcut icon" type="image/png" href="./favicon.png" />
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.50.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.50.0/mapbox-gl.css' rel='stylesheet' />
<style>
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
.mapboxgl-popup {
max-width: 400px;
border-color: #FF4757;
padding: 1em;
font: 12px/20px 'Rubik';
}
.navigation {
z-index: 9999;
padding: 1em;
font-family: 'Rubik';
font-weight: 700;
border: none;
border-radius: 2px;
font-size: 1em;
color: #fff;
background-color: #4898FF;
position: absolute;
bottom: 2em;
opacity: 0;
transition-timing-function: ease-in-out;
transition-duration: 0.3s;
transition-property: opacity;
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<a href="navigation.html"><button class="navigation">Start Navigation</button></a>
<div id='map'></div>
<script>
(async function () {
function fetchJSON(url, options) {
return fetch(url, options).then(function (response) {
return response.json().then(function (data) {
return {
status: response.status,
headers: response.headers,
json: data
}
})
})
}
window.setTimeout(function () {
document.getElementsByClassName('navigation')[0].style.opacity = 1;
}, 1000)
let data1 = await fetchJSON(
'https://api.mapbox.com/directions/v5/mapbox/driving/81.7686003%2C25.430287%3B81.8229763%2C25.4452978.json?access_token=pk.eyJ1IjoiZ3V5d2hvZGVzaWducyIsImEiOiJjam43ajRxbW4wYWV4M3Fwank4dTNsZzduIn0.ipHstqtP2KWs2NNPNJThgA&geometries=geojson'
).then((r) => r.json);
let data2 = await fetchJSON(
'https://api.mapbox.com/directions/v5/mapbox/driving/81.7686003%2C25.430287%3B81.802659%2C25.435606%3B81.8229763%2C25.4452978.json?access_token=pk.eyJ1IjoiZ3V5d2hvZGVzaWducyIsImEiOiJjam43ajRxbW4wYWV4M3Fwank4dTNsZzduIn0.ipHstqtP2KWs2NNPNJThgA&geometries=geojson'
).then((r) => r.json);
// console.log(data2);
mapboxgl.accessToken =
'pk.eyJ1IjoiZ3V5d2hvZGVzaWducyIsImEiOiJjam43ajRxbW4wYWV4M3Fwank4dTNsZzduIn0.ipHstqtP2KWs2NNPNJThgA';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/dark-v9',
center: [81.791422, 25.435019],
zoom: 14
});
const data3 = data1.routes[0].geometry.coordinates.slice(0, 7);
const data4 = data1.routes[0].geometry.coordinates.slice(6, 8);
const data5 = data1.routes[0].geometry.coordinates.slice(7);
const data6 = data2.routes[0].geometry.coordinates.slice(0, 5);
const data7 = data2.routes[0].geometry.coordinates.slice(4, 7);
const data8 = data2.routes[0].geometry.coordinates.slice(6);
// data3.routes[0].geometry.coordinates = data1.routes[0].geometry.coordinates.slice(0,9)
// data4.routes[0].geometry.coordinates = data1.routes[0].geometry.coordinates.slice(9, 11)
// data5.routes[0].geometry.coordinates = data1.routes[0].geometry.coordinates.slice(11)
var layerAdd = (color, id, coordinate, stroke) => {
map.on('load', function () {
// console.log('add ' + id)
map.addLayer({
"id": id,
"type": "line",
"source": {
"type": "geojson",
"data": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": coordinate
}
}
},
"layout": {
"line-join": "round",
"line-cap": "round"
},
"paint": {
"line-color": color,
"line-width": stroke
}
});
})
}
layerAdd('#4898FF', 'route3', data3, 3)
// layerAdd('#8333F0', 'route2', data2.routes[0].geometry.coordinates, 3)
layerAdd('#4898FF', 'route5', data5, 3)
layerAdd('#FF4757', 'route4', data4, 6)
layerAdd('#2FD86D', 'route6', data6, 3)
layerAdd('#FF4757', 'route7', data7, 6)
layerAdd('#2FD86D', 'route8', data8, 3)
// Create a popup, but don't add it to the map yet.
var popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
map.on('mouseenter', 'route4', function (e) {
// Change the cursor style as a UI indicator.
map.getCanvas().style.cursor = 'pointer';
var coordinates = [(data4[0][0] + data4[1][0]) / 2, (data4[0][1] + data4[1][1]) / 2];
console.log(data4);
var description = 'This route has damaged roads.';
popup.setLngLat(coordinates)
.setHTML(description)
.addTo(map);
});
map.on('mouseleave', 'route4', function () {
map.getCanvas().style.cursor = '';
popup.remove();
});
map.on('mouseenter', 'route7', function (e) {
// Change the cursor style as a UI indicator.
map.getCanvas().style.cursor = 'pointer';
var coordinates = [(data7[0][0] + data7[1][0]) / 2, (data7[0][1] + data7[1][1]) / 2];
var description = 'This route has damaged roads.';
popup.setLngLat(coordinates)
.setHTML(description)
.addTo(map);
});
map.on('mouseleave', 'route7', function () {
map.getCanvas().style.cursor = '';
popup.remove();
});
})()
</script>
</body>
</html>