-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap_test.html
187 lines (161 loc) · 10 KB
/
map_test.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
<!DOCTYPE html>
<html>
<head>
<title>Route Map</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
#map-canvas {
z-index: 4;
}
.overlay {
color: rgba(0,0,0,.6);
z-index: 10000;
height: 100%;
width: 100%;
position: fixed;
display: block;
}
</style>
</head>
<body>
<div id="map-canvas"></div>
<div class="overlay"></div>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
var latlng = new google.maps.LatLng(37.7858224, -122.4244381)
var mapStyle = [
{"featureType": "water","elementType": "geometry","stylers": [{ "hue": "#0066ff" },{ "lightness": -20 },{ "saturation": -33 },{ "gamma": 0.77 }]},
{"featureType": "water","elementType": "labels","stylers": [{ "visibility": "off" }]},
{"featureType": "poi.park","elementType": "geometry","stylers": [{ "hue": "#00ff00" },{ "lightness": 58 },{ "gamma": 0.3 },{ "saturation": 4 }]},
{"featureType": "transit","stylers": [{ "visibility": "off" }]},
{"featureType": "poi.business", "elementType": "labels", "stylers": [{ "visibility": "off" }]},
{"featureType": "road","elementType": "geometry.stroke","stylers": [{ "visibility": "off" }]},
{"featureType": "road.highway.controlled_access","stylers": [{ "visibility": "simplified" }]},
{"featureType": "road.highway","elementType": "geometry","stylers": [{ "hue": "#ffbb00" },{ "saturation": -5 },{ "lightness": 47 }, { "gamma":.49 }]},
{"featureType": "road.highway.controlled_access","elementType": "geometry","stylers": [{ "hue": "#ffbb00" },{ "saturation": -5 },{ "lightness": 47 }, { "gamma":1 }]},
{"featureType": "road.highway.controlled_access","elementType": "labels","stylers": [{ "visibility": "off" }]},
{"featureType": "poi.park","stylers": [{"visibility": "simplified"}]},
{"featureType": "poi.school","stylers": [{"visibility": "simplified"}]},
{"featureType": "administrative.neighborhood","stylers": [{"visibility": "off"}]},
];
var mapOptions = {
zoom: 14,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: mapStyle
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var rendererOptions = {
map: map,
// markerOptions: { visible: false },
// draggable: true,
suppressMarkers: true,
polylineOptions: {
strokeColor: "#ff620c", //ff8e2b
strokeWeight: 7,
strokeOpacity: .75
}
};
directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
directionsService = new google.maps.DirectionsService();
var stops = [
[37.7863283,-122.4238018],[37.7867987,-122.423294],[37.786868,-122.423308],[37.7870792,-122.4217463],[37.7866033,-122.4216557],[37.7866305,-122.4214926],[37.7871018,-122.4215934],[37.7875696,-122.4216863],[37.7880158,-122.4217752],[37.7894205,-122.4220632],[37.7894028,-122.4222174],[37.7903247,-122.422408],[37.7903957,-122.4224233],[37.793046,-122.4229546],[37.7939255,-122.4231393],[37.7948059,-122.4233028],[37.7957216,-122.423483],[37.7966779,-122.4236805],[37.7976138,-122.4238693],[37.7985409,-122.4240576],[37.7994737,-122.4242472],[37.7992746,-122.4258137],[37.8002087,-122.4260016],[37.8001005,-122.4268504],[37.799789,-122.4292902],[37.7995797,-122.4309351],[37.8004265,-122.431119],[37.8002135,-122.4327673],[37.8000101,-122.4344061],[37.7998024,-122.4360449],[37.7996533,-122.4372055],[37.7995915,-122.4376864],[37.7996837,-122.4377039],[37.799476,-122.4393481],[37.7992651,-122.4409963],[37.7990516,-122.4426583],[37.7988475,-122.444336],[37.7988809,-122.4447475],[37.7988338,-122.4449686],[37.7987225,-122.4451725],[37.798626,-122.445974],[37.7984672,-122.447331],[37.7975146,-122.4471579],[37.7976894,-122.4457849],[37.7987422,-122.437514],[37.798278,-122.43742],[37.796879,-122.437138],[37.795949,-122.43695],[37.7940594,-122.4365735],[37.793137,-122.436381],[37.7922619,-122.4362118],[37.7917904,-122.4366029],[37.7916854,-122.4369327],[37.7910478,-122.4368212],[37.7910056,-122.4370911],[37.7909192,-122.4376212],[37.7907865,-122.4375943],[37.7902949,-122.4374992],[37.790081,-122.439136],[37.789208,-122.43896],[37.7890065,-122.4405805],[37.7887852,-122.4423142],[37.7878623,-122.4421113],[37.785968,-122.441726],[37.7857554,-122.4433537],[37.785968,-122.441726],[37.786176,-122.4401062],[37.7861817,-122.4400263],[37.786811,-122.435106],[37.785876,-122.434918],[37.7849462,-122.4347304],[37.7850478,-122.433905],[37.7853696,-122.4312915],[37.785564,-122.429783],[37.7856664,-122.4290218],[37.7856858,-122.4288775],[37.785784,-122.428148],[37.7858647,-122.4275034],[37.786196,-122.424858]
];
var batches = [];
var itemsPerBatch = 10; // google API max - 1 start, 1 stop, and 8 waypoints
var itemsCounter = 0;
var wayptsExist = stops.length > 0;
while (wayptsExist) {
var subBatch = [];
var subitemsCounter = 0;
for (var j = itemsCounter; j < stops.length; j++) {
subitemsCounter++;
subBatch.push({
location: new window.google.maps.LatLng(stops[j][0], stops[j][1]),
stopover: false
});
if (subitemsCounter == itemsPerBatch)
break;
}
itemsCounter += subitemsCounter;
batches.push(subBatch);
wayptsExist = itemsCounter < stops.length;
// If it runs again there are still points. Minus 1 before continuing to
// start up with end of previous tour leg
itemsCounter--;
}
// function calcRoute(batches, directionsService, directionsDisplay) {
var combinedResults;
var unsortedResults = [{}]; // to hold the counter and the results themselves as they come back, to later sort
var directionsResultsReturned = 0;
for (var k = 0; k < batches.length; k++) {
var lastIndex = batches[k].length - 1;
var start = batches[k][0].location;
var end = batches[k][lastIndex].location;
// trim first and last entry from array
var waypts = [];
waypts = batches[k];
waypts.splice(0, 1);
waypts.splice(waypts.length - 1, 1);
var request = {
origin : start,
destination : end,
waypoints : waypts,
travelMode : window.google.maps.TravelMode.WALKING
};
(function (kk) {
directionsService.route(request, function (result, status) {
if (status == window.google.maps.DirectionsStatus.OK) {
var unsortedResult = {
order : kk,
result : result
};
unsortedResults.push(unsortedResult);
directionsResultsReturned++;
console.log(unsortedResult)
console.log(directionsResultsReturned)
console.log(batches.length)
if (directionsResultsReturned == batches.length || directionsResultsReturned == 10) // we've received all the results. put to map
{
// sort the returned values into their correct order
unsortedResults.sort(function (a, b) {
return parseFloat(a.order) - parseFloat(b.order);
});
var count = 0;
for (var key in unsortedResults) {
if (unsortedResults[key].result != null) {
if (unsortedResults.hasOwnProperty(key)) {
if (count == 0) // first results. new up the combinedResults object
combinedResults = unsortedResults[key].result;
else {
// only building up legs, overview_path, and bounds in my consolidated object. This is not a complete
// directionResults object, but enough to draw a path on the map, which is all I need
combinedResults.routes[0].legs = combinedResults.routes[0].legs.concat(unsortedResults[key].result.routes[0].legs);
combinedResults.routes[0].overview_path = combinedResults.routes[0].overview_path.concat(unsortedResults[key].result.routes[0].overview_path);
combinedResults.routes[0].bounds = combinedResults.routes[0].bounds.extend(unsortedResults[key].result.routes[0].bounds.getNorthEast());
combinedResults.routes[0].bounds = combinedResults.routes[0].bounds.extend(unsortedResults[key].result.routes[0].bounds.getSouthWest());
}
count++;
}
}
}
directionsDisplay.setDirections(combinedResults);
}
}
});
})(k);
}
// }
};
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</body>
</html>