-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.html
186 lines (184 loc) · 6.35 KB
/
index.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
<!--
-
- @Name : TaxiBookingMap/index.html
- @Version : 1.0
- @Programmer : Max
- @Date : 2019-07-09
- @Released under : https://github.com/BaseMax/TaxiBookingMap/blob/master/LICENSE
- @Repository : https://github.com/BaseMax/TaxiBookingMap
-
-->
<!DOCTYPE html>
<html>
<head>
<title>Booking Taxi</title>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
#form {
margin-top: 50px;
}
#map {
width: 100%;
height: 300px;
margin-top: 20px;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<!-- left col -->
<div id="form" class="form-horizontal col-sm-8">
<div class="form-group">
<b><label for="from-input">From</label></b>
<input type="search" class="form-control" id="from-input" placeholder="From">
<!-- aria-describedby -->
</div>
<div class="form-group">
<b><label for="to-input">To</label></b>
<input type="search" class="form-control" id="to-input" placeholder="To">
</div>
<div class="form-group">
<b><label for="to-input">Step At</label></b>
<input type="text" class="form-control" placeholder="Step At">
</div>
<button id="submit" type="submit" class="btn btn-primary">Booking</button>
</div>
<!-- right col -->
<div id="form" class="form-horizontal col-sm-4">
<div class="form-group">
<b><label for="to-input">Distance</label></b>
<label id="value-distance" class="form-text">Press on the Booking button to calculate...</label>
</div>
<div class="form-group">
<b><label for="to-input">Price per KM</label></b>
<label id="price" class="form-text">5 USD</label>
</div>
<div class="form-group">
<b><label for="to-input">Total Price</label></b>
<label id="value-price" class="form-text">Press on the Booking button to calculate...</label>
</div>
</div>
</div>
</div>
<div id="map"></div>
<script>
var markers = [];
var locations = [];
var map;
var distanceDirection;
var distanceDisplay;
function string2Location(input, index) {
var searchBox = new google.maps.places.SearchBox(input);
// map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
distanceDirection = new google.maps.DirectionsService();
distanceDisplay = new google.maps.DirectionsRenderer();
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if(places.length == 0) {
return;
}
markers[index].setMap(null);
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)
};
markers[index]=new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
});
if(index == 0) {
locations[index]=place.geometry.location;
}
else {
locations[index]=place.geometry.location;
}
// console.log(locations[index].lat() + ", " + locations[index].lng());
if(place.geometry.viewport) {
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
distanceDisplay.setMap(map);
});
}
function initAutocomplete() {
locations[0] = new google.maps.LatLng();
locations[1] = new google.maps.LatLng();
markers[0] = new google.maps.Marker();
markers[1] = new google.maps.Marker();
map = new google.maps.Map(document.getElementById('map'), {
// Default position...
center: {
lat: 35.0823294,
lng: -106.8165662
},
zoom: 13,
// https://developers.google.com/maps/documentation/javascript/maptypes
mapTypeId: 'roadmap', // https://developers.google.com/maps/documentation/gaming/support/roadmap
});
var from = document.getElementById('from-input');
var to = document.getElementById('to-input');
string2Location(from, 0);
string2Location(to, 1);
google.maps.event.addDomListener(document.getElementById('submit'), 'click', calcRoute);
}
function calcDistance(start, end) {
return(google.maps.geometry.spherical.computeDistanceBetween(start, end) / 1000).toFixed(2); //KM
}
function calcRoute() {
var value_distance=document.querySelector("#value-distance");
var value_price=document.querySelector("#value-price");
var start = new google.maps.LatLng(locations[0].lat(), locations[0].lng());
var end = new google.maps.LatLng(locations[1].lat(), locations[1].lng());
var distance=calcDistance(start, end);
value_distance.innerHTML=distance;
value_price.innerHTML=(distance * 5) + " USD"; // We can use .toFixed()...
var bounds = new google.maps.LatLngBounds();
bounds.extend(start);
bounds.extend(end);
map.fitBounds(bounds);
var request = {
travelMode: google.maps.TravelMode.DRIVING,
origin: start,
destination: end,
};
distanceDirection.route(request, function(response, status) {
if(status == google.maps.DirectionsStatus.OK) {
distanceDisplay.setDirections(response);
distanceDisplay.setMap(map);
}
else {
alert("Error: From " + start.toUrlValue(6) + " to " + end.toUrlValue(6) + " failed : " + status);
}
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initAutocomplete" async defer></script>
</body>
</html>