-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
264 lines (233 loc) · 8.87 KB
/
index.html
File metadata and controls
264 lines (233 loc) · 8.87 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Mapping multiple routes with Mapbox</title>
<meta name='robots' content='noindex, nofollow'>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.2.0/mapbox-gl.js'></script>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.2.0/mapbox-gl.css' rel='stylesheet' />
<link href='/css/styles.css' rel='stylesheet' />
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-139598622-4"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-139598622-4');
</script>
</head>
<body>
<div id='map'></div>
<script src='//api.tiles.mapbox.com/mapbox.js/plugins/turf/v2.0.0/turf.min.js' charset='utf-8'></script>
<script src='https://unpkg.com/es6-promise@4.2.4/dist/es6-promise.auto.min.js'></script>
<script src="https://unpkg.com/@mapbox/mapbox-sdk/umd/mapbox-sdk.min.js"></script>
<script>
/**
* Initial Mapbox configuration
*/
var accessToken = 'pk.eyJ1IjoibWJlamRhIiwiYSI6ImNqb2RnMDJjdzJrdjIzcHJzeDE2bHk2Zm8ifQ.-dJu7dSO9-uj37Pf5jg10A';
mapboxgl.accessToken = accessToken;
/**
* Our API abstraction
* @type {{init: api.init, addRoute: (function(*, *=, *=): Promise<any>), geocode(*=): *, addMarker(*=, *, *): void}}
*/
var api = {
map: new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [-81.357496,28.555964],
zoom: 12,
}),
client: mapboxSdk({ accessToken: mapboxgl.accessToken }),
addControls: function(){
this.map.addControl(new mapboxgl.NavigationControl());
},
/**
* Each route will have a start point, endpoint and a color and name.
* @param path
* @param color
* @param routeName
* @returns {Promise<any>}
*/
addRoute: function(path, color, routeName){
var self = this;
/**
* Adds route to our map
* */
return new Promise(function(resolve, reject){
/***
* Get the directions for each path
* */
self.client
.directions
.getDirections({
profile: 'driving',
geometries: 'geojson',
waypoints: [
{
coordinates: path[0]
},
{
coordinates: path[1]
}
]
}).send()
.then(function(response) {
const data = response.body;
if (data.code === "NoRoute") {
return reject(data);
}
var coord = data.routes[0].geometry.coordinates;
var lineColor = color || "#FFA500";
/***
* Add route layer to our map
* */
self.map.addLayer({
"id": routeName,
"type": "line",
"source": {
"type": "geojson",
"data": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": coord
}
}
},
"layout": {
"line-join": "round",
"line-cap": "round"
},
"paint": {
"line-color": lineColor,
"line-width": 5,
"line-opacity": .70
}
});
resolve(data);
});
});
},
/**
* Use the mapbox geocode client to geocode our addresses
* @param query
* @returns {Promise<any>}
*/
geocode: function(query){
return this.client.geocoding.forwardGeocode({
query: query,
autocomplete: false,
limit: 1
}).send();
},
/**
* Add marker to map
* @param coordinates
* @param imgSource
* @param cls
*/
addMarker: function(coordinates, imgSource, cls){
var elem = document.createElement("div");
var img = new Image();
img.src= imgSource;
img.className = cls;
elem.appendChild(img);
new mapboxgl.Marker(elem)
.setLngLat(coordinates)
.addTo(this.map);
}
};
api.map.on('load', function () {
/***
* Add map controls to the map
* */
api.addControls();
/***
* Geocode our locations
* and create a bounding box
* */
Promise.all([
api.geocode('1850 Illinois Street, Orlando, Florida 32803, United States'),
api.geocode('6000 Universal Blvd, Orlando, FL'),
api.geocode('14501 S Orange Blossom Trail, Orlando, FL 32837'),
api.geocode('Kennedy Space Center'),
api.geocode('3755 W Seminole Blvd, Sanford, FL 32771')
]).then(function(responses){
/**
* Make sure all of our markers display on the map.
*/
var boundingBox = new mapboxgl.LngLatBounds();
var bounds = [];
responses.forEach(function(response){
var center = response.body.features[0].center;
boundingBox.extend(center);
bounds.push(center);
});
api.map.fitBounds(boundingBox,{
padding: {top: 100, bottom:100, left: 100, right: 100}
});
/**
* Our first bound is the starting point
* @type {Object}
*/
var start = bounds.shift();
/**
* Give the starting point a marker
*/
api.addMarker(
start,
'https://image.flaticon.com/icons/svg/1235/1235172.svg',
'icon swing'
);
/**
* Add routes to the map
* This could be in a loop....
*/
api.addRoute([start,bounds[0]], '#8A2BE2', 'universal')
.then(function(data){
/**
* The last waypoint is always our destination.
*/
var coords = data.waypoints.pop().location;
api.addMarker(
coords,
'https://image.flaticon.com/icons/svg/1999/1999062.svg',
'icon miniBounce'
);
});
api.addRoute([start,bounds[1]], '#00c78c', 'gatorland')
.then(function(data){
var coords = data.waypoints.pop().location;
api.addMarker(
coords,
'https://image.flaticon.com/icons/svg/1998/1998620.svg',
'icon rotate'
);
});
api.addRoute([start,bounds[2]], '#FFA500', 'spacecenter')
.then(function(data){
var coords = data.waypoints.pop().location;
api.addMarker(
coords,
'https://image.flaticon.com/icons/svg/321/321805.svg',
'icon rotate-2'
);
});
api.addRoute([start,bounds[3]], '#FF00F6', 'zoo')
.then(function(data){
var coords = data.waypoints.pop().location;
api.addMarker(
coords,
'https://image.flaticon.com/icons/svg/194/194978.svg',
'icon rotate-2-flip'
);
});
});
});
</script>
</body>
</html>