-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
210 lines (191 loc) · 5.81 KB
/
map.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
var map;
var service;
var geocoder;
var latitude = 32.985771;
var longitude = -96.750003;
var directionsService;
var directionsDisplay;
var gmarkers = [];
var points = [];
function addtoPoints(toAdd) {
points.push(toAdd);
}
function openWindow() {
var urlParams = "";
for (var i = 0; i < points.length; i++) {
urlParams += points[i].lat + "," + points[i].lng + "/";
}
var url = "https://www.google.com/maps/dir/" + urlParams;
window.open(url);
}
async function initialize() {
geocoder = new google.maps.Geocoder();
showMap(37, -96, 4);
directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();
}
const getAddress = (address) => {
return new Promise((resolve, reject) => {
geocoder.geocode({ address: address }, (results, status) => {
if (status === "OK") {
resolve(results[0]);
} else {
reject(status);
}
});
});
};
const getRoute = (request) => {
return new Promise((resolve, reject) => {
directionsService.route(request, (results, status) => {
if (status === "OK") {
resolve(results);
} else {
reject(status);
}
});
});
};
const getStops = (service, stopRequest) => {
return new Promise((resolve, reject) => {
service.nearbySearch(stopRequest, (results, status) => {
if (status === "OK") {
resolve(results[0]);
} else if (status === "OVER_QUERY_LIMIT") {
reject(status);
alert("Over query limit");
} else {
reject(status);
}
});
});
};
async function showRoute() {
for (i = 0; i < gmarkers.length; i++) {
gmarkers[i].setMap(null);
}
var list = document.getElementById("your_stops");
while (list.firstChild) {
list.removeChild(list.firstChild);
}
var mapsButton = document.getElementById("maps_button");
while (mapsButton.firstChild) {
mapsButton.removeChild(mapsButton.firstChild);
}
points = [];
var stopType = document.querySelector('input[name="stop_type"]:checked').id;
var start = document.getElementById("start").value;
var dest = document.getElementById("dest").value;
//Get coordinates of route endpoints
let startPoint = await getAddress(start);
points.push({
lat: startPoint.geometry.location.lat(),
lng: startPoint.geometry.location.lng(),
});
//Get directions
directionsDisplay.setMap(map);
//map.clearOverlays();
var request = {
origin: start,
destination: dest,
travelMode: "DRIVING",
};
let directionResult = await getRoute(request);
directionsDisplay.setDirections(directionResult);
document.getElementById("distance").innerHTML =
"<strong> Total Distance:</strong> " +
directionResult.routes[0].legs[0].distance.text;
document.getElementById("duration").innerHTML =
"<strong>Total Duration:</strong> " +
directionResult.routes[0].legs[0].duration.text;
//Finding "stops"
var steps = directionResult.routes[0].legs[0].steps;
var sumDuration = 0;
var stops = [];
if (steps.length > 1) {
for (var i = 0; i < steps.length - 1; i++) {
if (steps[i].duration.value >= 7200) {
sumDuration += steps[i].duration.value / 2;
var end_location = {
lat:
(steps[i].start_location.lat() + steps[i].end_location.lat()) / 2,
lng:
(steps[i].start_location.lng() + steps[i].end_location.lng()) / 2,
};
stops.push(new google.maps.LatLng(end_location.lat, end_location.lng));
sumDuration = 0;
} else {
sumDuration += steps[i].duration.value;
if (sumDuration + steps[i + 1].duration.value >= 7200) {
stops.push(steps[i].end_location);
sumDuration = 0;
} else if (sumDuration >= 5400) {
stops.push(steps[i].end_location);
sumDuration = 0;
}
}
}
}
for (const stop of stops) {
// new google.maps.Marker({
// map,
// title: "Stop " + i.toString(),
// position: {
// lat: stop.lat(),
// lng: stop.lng(),
// },
// });
//Getting nearby gas station
var stopRequest = {
location: new google.maps.LatLng(stop.lat(), stop.lng()),
radius: "32000",
type: stopType,
rankby: "prominence",
};
service = new google.maps.places.PlacesService(map);
let stopsPoints = await getStops(service, stopRequest);
var marker = new google.maps.Marker({
map,
title: stopsPoints.name,
position: {
lat: stopsPoints.geometry.location.lat(),
lng: stopsPoints.geometry.location.lng(),
},
});
var list = document.getElementById("your_stops");
var entry = document.createElement("li");
entry.classList.add("list-group-item");
entry.appendChild(document.createTextNode(stopsPoints.name));
list.appendChild(entry);
gmarkers.push(marker);
points.push({
lat: stopsPoints.geometry.location.lat(),
lng: stopsPoints.geometry.location.lng(),
});
}
let endPoint = await getAddress(dest);
points.push({
lat: endPoint.geometry.location.lat(),
lng: endPoint.geometry.location.lng(),
});
const stats = document.getElementById("maps_button");
// creating the span element, then add a class attribute
const button = document.createElement("button");
button.setAttribute("class", "btn btn-primary");
button.innerHTML = "Show Route on Google Maps";
button.onclick = openWindow;
stats.appendChild(button);
// var urlParams = "";
// for (var i = 0; i < points.length; i++) {
// urlParams += points[i].lat + "," + points[i].lng + "/";
// }
// var url = "https://www.google.com/maps/dir/" + urlParams;
// window.open(url);
}
function showMap(latitude, longitude, zoom) {
var place = new google.maps.LatLng(latitude, longitude);
map = new google.maps.Map(document.getElementById("map"), {
center: place,
zoom: zoom,
});
}