-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
317 lines (270 loc) · 8.5 KB
/
Copy pathscript.js
File metadata and controls
317 lines (270 loc) · 8.5 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
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
var placesToRandom = 5;
var rand_lat;
var rand_lng;
function init() {
initMap();
initSearchBox();
}
function getLocation(callback) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
if (callback != null && callback !== undefined) callback(pos);
window.currentLocation = pos;
return;
}, function () {
});
}
return null;
}
function Locationize(latitute, longitude) {
return {
lat: latitute,
lng: longitude
};
}
function displayInfo(pos, message) {
var infoWindow = new google.maps.InfoWindow;
infoWindow.setPosition(pos);
infoWindow.setContent(message);
infoWindow.open(map);
}
function drawLine() {
var locs = [];
for (var i = 0; i < arguments.length; i++) {
locs.push(arguments[i]);
}
var PolylinePath = new google.maps.Polyline({
path: locs, // From a to b
geodesic: false,
strokeColor: '#FF2222',
strokeOpacity: 1.0,
strokeWeight: 2
});
PolylinePath.setMap(map);
}
function combineLocation(loc1, loc2) {
return {
lat: loc1.lat + loc2.lat,
lng: loc1.lng + loc2.lng
};
}
function initMap() {
var centerLocation = {
lat: 13.736717,
lng: 100.523186
};
getLocation(function (loc) {
if (loc != null)
centerLocation = loc;
})
map = new google.maps.Map(document.getElementById('map'), {
center: centerLocation,
zoom: 18,
mapTypeId: 'roadmap',
disableDefaultUI: false,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
position: google.maps.ControlPosition.LEFT_BOTTOM
},
zoomControl: true,
zoomControlOptions: {
position: google.maps.ControlPosition.LEFT_CENTER
},
scaleControl: true,
streetViewControl: true,
streetViewControlOptions: {
position: google.maps.ControlPosition.LEFT_TOP
},
fullscreenControl: true
});
directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();
//directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true});
directionsDisplay.setMap(map);
getLocation(); //Fetch my location
}
function initSearchBox() {
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
map.controls[google.maps.ControlPosition.TOP_CENTER].push(input);
var searchBox = new google.maps.places.SearchBox(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function () {
searchBox.setBounds(map.getBounds());
});
var markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function () {
var places = searchBox.getPlaces();
if (places.length === 0) {
return;
}
selectedPlaceUpdated(places);
// Clear out the old markers.
markers.forEach(function (marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function (place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}
function generateWaypointsFromLocation() {
var waypoints = [];
for (var i = 0; i < arguments.length; i++) {
waypoints.push({
location: arguments[i],
stopover: false
});
}
return waypoints;
}
function bugSprayOnStupidDot() {
setTimeout(function () {
clearStupidDot();
}, 1);
setTimeout(function () {
clearStupidDot();
}, 2);
setTimeout(function () {
clearStupidDot();
}, 5);
setTimeout(function () {
clearStupidDot();
}, 10);
setTimeout(function () {
clearStupidDot();
}, 30);
setTimeout(function () {
clearStupidDot();
}, 60);
setTimeout(function () {
clearStupidDot();
}, 120);
setTimeout(function () {
clearStupidDot();
}, 250);
setTimeout(function () {
clearStupidDot();
}, 500);
setTimeout(function () {
clearStupidDot();
}, 1000);
}
function clearStupidDot() {
var imgs = document.getElementsByTagName("img");
for (var i = 0; i < imgs.length; i++) {
if (imgs[i].src === "https://maps.gstatic.com/mapfiles/dd-via.png") {
imgs[i].style.visibility = "hidden";
}
}
}
function calcRoute(_origin, _destination, _waypoints, showonmap, callback) {
var request = {
origin: _origin,
destination: _destination,
waypoints: _waypoints,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
callbackValid = (callback !== undefined && callback != null);
if (status === 'OK') {
window.directionQueryResponse = response;
if (showonmap !== false) directionsDisplay.setDirections(response);
window.response = response;
bugSprayOnStupidDot();
if (callbackValid) callback(true);
}
else {
callback(false);
}
});
}
function selectedPlaceUpdated(places) {
if (places.length !== 1) return;
getLocation();
loc2 = Locationize(places[0].geometry.location.lat(), places[0].geometry.location.lng());
calcRoute(currentLocation, loc2, undefined, false, function (woo) {
if (woo) {
window.shortestRouteDistance = directionQueryResponse.routes[0].legs[0].distance;
}
});
generateStupidRoute(false);
}
function randomLocation() {
rand_lat = Math.floor(Math.random() * 7) - 3;
rand_lng = Math.floor(Math.random() * 7) - 3;
if (rand_lat === 0 || rand_lng === 0) {
randomLocation();
return;
}
}
//exit variable use for concurrent call
function generateStupidRoute(exit) {
if (exit === true) {
newDistance = directionQueryResponse.routes[0].legs[0].distance;
setTimeout(function () {
alert("Hello there!\n" +
"Originally you will have to travel for " + shortestRouteDistance.text + "\n" +
"With our advanced algorithm you'll have to travel for just " + newDistance.text + "\n" +
"We saved you " + ((shortestRouteDistance.value - newDistance.value) / 1000) + " km" + "\n" +
"This shit is impressive, isn't it?"
);
}, 1000);
return;
}
//Generate random waypoints
var waypoints = [];
for (var i = 0; i < placesToRandom; i++) {
randomLocation();
waypoints.push({
location: combineLocation(currentLocation, Locationize(rand_lat, rand_lng)),
stopover: false
});
}
//Pass generateStupidRoute as a callback so if route generation failed, it will re-random and try to find new one
calcRoute(currentLocation, loc2, waypoints, true, generateStupidRoute);
}
function startButton() {
getLocation(function (a) {
displayInfo(window.currentLocation, 'YOU ARE HERE');
});
}
/*function popupAds() {
var popup = document.getElementById("myPopup");
popup.classList.toggle("show");
}
</script>*/