-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombined_sample.js
211 lines (198 loc) · 8.06 KB
/
combined_sample.js
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
var myApp = angular.module("myApp",[]);
myApp.controller("MainController", function(){
this.text = "hello world";
this.requestArray = [];
this.renderArray = [];
this.commonWaypoints = [];
this.colourArray = ['navy', 'grey', 'fuchsia', 'black', 'white', 'lime', 'maroon', 'purple', 'aqua', 'red', 'green', 'silver', 'olive', 'blue', 'yellow', 'teal'];
this.getData = function(form){
console.log(this.pointA, this.pointB, this.pointC, this.pointD);
this.requestArray.push({
request: {
origin: this.pointA,
destination: this.pointB,
avoidTolls: true,
avoidHighways: false,
travelMode: google.maps.TravelMode.DRIVING
}
},{
request: {
origin: this.pointC,
destination: this.pointD,
avoidTolls: true,
avoidHighways: false,
travelMode: google.maps.TravelMode.DRIVING
}
})
console.log(this.requestArray);
this.processRequests();
}
this.processRequests = function(){
// Counter to track request submission and process one at a time;
var i = 0;
var that = this;
// Used to submit the request 'i'
function submitRequest(){
console.log('requesting', i);
that.directionsService.route(that.requestArray[i].request, directionResults);
}
// Used as callback for the above request for current 'i'
function directionResults(result, status) {
console.log('results', i, result);
if (status == google.maps.DirectionsStatus.OK) {
// Create a unique DirectionsRenderer 'i'
that.renderArray[i] = new google.maps.DirectionsRenderer();
that.renderArray[i].setMap(that.map);
// Some unique options from the colorArray so we can see the routes
that.renderArray[i].setOptions({
preserveViewport: true,
suppressInfoWindows: true,
polylineOptions: {
strokeWeight: i == 2 ? 6 : 4,
strokeOpacity: i == 2 ? 1 : 0.8,
strokeColor: that.colourArray[i]
},
markerOptions:{
icon:{
path: google.maps.SymbolPath.BACKWARD_CLOSED_ARROW,
scale: 3,
strokeColor: that.colourArray[i]
}
}
});
console.log(result);
// Use this new renderer with the result
that.renderArray[i].setDirections(result);
// and start the next request
if (i == 1) {
var array1 = that.renderArray[0].directions.routes[0].overview_path;
var array2 = that.renderArray[1].directions.routes[0].overview_path;
array1 = array1.map(function(point){
return {
lat: point.lat().toFixed(2),
lng: point.lng().toFixed(2),
}
});
array2 = array2.map(function(point){
return {
lat: point.lat().toFixed(2),
lng: point.lng().toFixed(2),
}
});
console.log(array2, array1, 'starting match');
array2.forEach(function(point2){
array1.forEach(function(point1){
if (point2.lat == point1.lat && point2.lng == point1.lng) {
that.commonWaypoints.push(point2);
}
});
});
console.log(that.commonWaypoints);
if (that.commonWaypoints.length) {
console.log(that.commonWaypoints[0].lat, that.commonWaypoints[0].lng);
console.log(that.commonWaypoints[that.commonWaypoints.length-1].lat,
that.commonWaypoints[that.commonWaypoints.length-1].lng);
that.commonPointA = new google.maps.LatLng(that.commonWaypoints[0].lat, that.commonWaypoints[0].lng);
that.commonPointB = new google.maps.LatLng(that.commonWaypoints[that.commonWaypoints.length-1].lat,
that.commonWaypoints[that.commonWaypoints.length-1].lng);
console.log(that.commonPointA, that.commonPointB);
that.requestArray.push({
request: {
origin: that.commonPointA,
destination: that.commonPointB,
avoidTolls: true,
avoidHighways: false,
travelMode: google.maps.TravelMode.DRIVING
}
});
console.log(that.requestArray);
}
}
nextRequest();
}
}
function nextRequest(){
// Increase the counter
i++;
console.log('continuing', i);
// Make sure we are still waiting for a request
if(i >= that.requestArray.length){
// No more to do
console.log('coming out', i);
return;
}
// Submit another request
submitRequest();
}
// This request is just to kick start the whole process
submitRequest();
}
this.initMap = function() {
console.log(google, this);
var that = this;
that.pointA = new google.maps.LatLng(22, 77);
that.myOptions = {
zoom: 7,
center: that.pointA
};
that.map = new google.maps.Map(document.getElementById('map-canvas'), that.myOptions);
// Instantiate a directions service.
that.directionsService = new google.maps.DirectionsService;
that.directionsDisplay = new google.maps.DirectionsRenderer({
map: that.map
});
var start1 = document.getElementById('start1');
var start2 = document.getElementById('start2');
var end1 = document.getElementById('end1');
var end2 = document.getElementById('end2');
var autocompleteStart1 = new google.maps.places.Autocomplete(start1);
autocompleteStart1.addListener('place_changed', function() {
that.start1 = autocompleteStart1.getPlace();
that.pointA = new google.maps.LatLng(that.start1.geometry.location.lat(),
that.start1.geometry.location.lng());
that.markerA = new google.maps.Marker({
position: that.pointA,
title: "point A",
label: "A",
map: that.map
});
});
var autocompleteStart2 = new google.maps.places.Autocomplete(start2);
autocompleteStart2.addListener('place_changed', function() {
that.start2 = autocompleteStart2.getPlace();
that.pointC = new google.maps.LatLng(that.start2.geometry.location.lat(),
that.start2.geometry.location.lng());
that.markerC = new google.maps.Marker({
position: that.pointC,
title: "point C",
label: "C",
map: that.map
});
});
var autocompleteEnd1 = new google.maps.places.Autocomplete(end1);
autocompleteEnd1.addListener('place_changed', function() {
that.end1 = autocompleteEnd1.getPlace();
that.pointB = new google.maps.LatLng(that.end1.geometry.location.lat(),
that.end1.geometry.location.lng());
that.markerB = new google.maps.Marker({
position: that.pointB,
title: "point B",
label: "B",
map: that.map
});
});
var autocompleteEnd2 = new google.maps.places.Autocomplete(end2);
autocompleteEnd2.addListener('place_changed', function() {
that.end2 = autocompleteEnd2.getPlace();
that.pointD = new google.maps.LatLng(that.end2.geometry.location.lat(),
that.end2.geometry.location.lng());
that.markerD = new google.maps.Marker({
position: that.pointD,
title: "point D",
label: "D",
map: that.map
});
});
}
this.initMap();
});