-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouteMap.html
More file actions
204 lines (189 loc) · 7.6 KB
/
Copy pathRouteMap.html
File metadata and controls
204 lines (189 loc) · 7.6 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<title>Route Map and ETA for your Pick-up points</title>
<style>
#left-panel {
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
#left-panel select, #left-panel input {
font-size: 15px;
}
#left-panel select {
width: 100%;
}
#left-panel i {
font-size: 12px;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
float: right;
width: 70%;
height: 100%;
}
#left-panel {
margin: 20px;
border-width: 2px;
width: 20%;
height: 400px;
float: right;
text-align: left;
padding-top: 0;
}
#directions-panel {
margin-top: 10px;
background-color: #FFEE77;
padding: 10px;
overflow: scroll;
height: 300px;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="left-panel">
<div>
<b>Starting Address:</b>
<select id="start">
<option value="Nagarabhavi">Nagarabhavi</option>
<option value="Sunkadakatte">Sunkadakatte</option>
<option value="Laggere">Laggere</option>
<option value="Yeshwanthpur">Yeshwanthpur</option>
</select>
<br><br>
<b>Pick-up points:</b><br>
<i>Select multiple pick-up points using Ctrl+Click</i>
<select multiple id="waypoints">
<option value="None">None</option>
<option value="Rajajinagar">Rajajinagar</option>
<option value="Srirampura">Srirampura</option>
<option value="Mantri Square">Mantri Square</option>
<option value="Richmond Town Park, Bengaluru">Richmond Town Park</option>
<option value="Koramangala">Koramangala</option>
<option value="Jayanagar">Jayanagar</option>
<option value="Café Coffee Day - Silk Board, Bengaluru">CCD Silk Board</Option>
</select>
<br><br>
<b>Destination Address:</b>
<select id="end">
<option value="Wipro Gate 11">Wipro Gate 11</option>
<option value="Bellandur">Bellandur</option>
<option value="International Tech Park Bangalore">International Tech Park Bangalore</option>
</select>
<br>
<input type="submit" id="submit" value="Find Route">
<br>
<input type="button" id="calculate-eta" value="Find ETA">
</div>
<div id="directions-panel"></div>
</div>
<script async defer src="http://maps.google.com/maps/api/js?sensor=true&callback=initMap"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
var map;
function initMap() {
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
// Center initialized to Bengaluru, India
map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: {lat: 12.97, lng: 77.56}
});
directionsDisplay.setMap(map);
document.getElementById('submit').addEventListener('click', function() {
displayRouteMap(directionsService, directionsDisplay);
});
}
// This function will call Google Direction Serivce API and finds the route map
function displayRouteMap(directionsService, directionsDisplay) {
var waypts = [];
var checkboxArray = document.getElementById('waypoints');
for (var i = 0; i < checkboxArray.length; i++) {
if (checkboxArray.options[i].selected) {
if(checkboxArray[i].value === 'None')
break;
waypts.push({
location: checkboxArray[i].value,
stopover: true
});
}
}
var originsArray = [];
var destinationsArray = [];
var summaryPanel = document.getElementById('directions-panel');
var startPoint = document.getElementById('start').value;
var endPoint = document.getElementById('end').value;
// Calling Google Map Direction serivces API to find the optimized Driving route
directionsService.route({
origin: startPoint,
destination: endPoint,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
var route = response.routes[0];
summaryPanel.innerHTML = '';
// For each route, display summary information.
for (var i = 0; i < route.legs.length; i++) {
var routeSegment = i + 1;
summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment +
'</b><br>';
summaryPanel.innerHTML += route.legs[i].start_address + ' to ';
summaryPanel.innerHTML += route.legs[i].end_address + '<br>';
summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';
}
} else {
window.alert('Directions request failed due to ' + status);
}
}); // directionsService.route
}
// This function is the event handler for the "Find ETA" button
$("#calculate-eta").click(function (event) {
event.preventDefault();
var distanceService = new google.maps.DistanceMatrixService();
var origin1 = document.getElementById('start').value;
var destinationA = document.getElementById('end').value;
var summaryPanel = document.getElementById('directions-panel');
summaryPanel.innerHTML = '';
summaryPanel.innerHTML += '<b> Origin </b>:' + origin1 + ' <br> <b>Destination</b>: ' + destinationA + '<br>';
distanceService.getDistanceMatrix( {
origins: [origin1],
destinations: [destinationA],
travelMode: 'DRIVING',
drivingOptions: {
departureTime: new Date(Date.now() + 100),
trafficModel: 'optimistic'
},
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false,
}, callback);
function callback(response, status){
if (status === 'OK') {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var results = response.rows[0].elements;
var element = results[0];
var distance = element.distance.text;
var duration = element.duration.text;
summaryPanel.innerHTML += '<b>Distance:</b> '+ distance + '<br>' + '<b>ETA:</b>'+ duration + '<br>';
}
else{
window.alert('Distance Service failed due to ' + status);
}
}
});
</script>
</body>
</html>