-
Notifications
You must be signed in to change notification settings - Fork 3
/
umap.html
215 lines (205 loc) · 8.07 KB
/
umap.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
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
<!DOCTYPE html >
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>ePark</title>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<input style="position: absolute;box-shadow: 1px 1px 20px rgb(161, 161, 161);padding:4px;opacity:0.8;width:100%;background:white;border:none;float:left;height: 32px;margin-top: 20%;" id="pac-input" class="controls" type="text" placeholder="Enter Destination">
<div id="map"></div>
<div id="conf" style="width:100%; height: 25%;">
<button>Submit</button>
</div>
<script>
document.querySelector("#conf").style.display = "none";
function initMap() {
var directionsDisplay = new google.maps.DirectionsRenderer({map: map});
var directionsService = new google.maps.DirectionsService;
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(10.056897,76.355665),
disableDefaultUI: true,
zoom: 16
});
//Directions
directionsDisplay.setMap(map);
//Search - Start
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(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;
}
// 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;
}
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
}
else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
//Search - Break
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP or XML file
downloadUrl('datadump.php', function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers, function(markerElem) {
var id = markerElem.getAttribute('id');
var name = markerElem.getAttribute('name');
//var address = markerElem.getAttribute('address');
var space = markerElem.getAttribute('space');
var total = markerElem.getAttribute('total');
var point = new google.maps.LatLng(
parseFloat(markerElem.getAttribute('lat')),
parseFloat(markerElem.getAttribute('lng')));
var infowincontent = document.createElement('div');
var strong = document.createElement('strong');
strong.textContent = name;
infowincontent.appendChild(strong);
infowincontent.appendChild(document.createElement('br'));
var text = document.createElement('text');
text.textContent = "Free spot : " + space + "/" + total;
infowincontent.appendChild(text);
var icon = space;
var marker = new google.maps.Marker({
map: map,
position: point,
label: icon
});
//Geolocation
navigator.geolocation.getCurrentPosition(function(position) {
var posCar = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
map.setCenter(posCar);
//icon
var image = 'car.png';
var locationMarker = new google.maps.Marker({
position: posCar,
map: map,
icon: image
})
//Geolocation end
directionsService.route({
origin: posCar,
destination: {lat: 10.052700, lng: 76.352127},
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
}
})
google.maps.event.addListener(marker,'click',function() {
alert('asdsadasd')
document.querySelector("#conf").style.visibility = "visible";
infoWindow.open(map, marker);
infoWindow.setContent(infowincontent);
var dest = marker.getPosition();
calculateAndDisplayRoute(directionsService, directionsDisplay,posCar,dest);
});
})
});
});
}
//Directions
function calculateAndDisplayRoute(directionsService, directionsDisplay, posCar, dest) {
directionsService.route({
origin: posCar,
destination: dest,
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
//distance
var distanceService = new google.maps.DistanceMatrixService();
distanceService.getDistanceMatrix({
origins: [posCar],
destinations: [dest],
travelMode: 'DRIVING',
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, function(response, status) {
if (status !== 'OK') {
alert('Error was: ' + status);
} else {
var originList = response.originAddresses;
var destinationList = response.destinationAddresses;
for (var i = 0; i < originList.length; i++) {
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
var element = results[j];
var distance = element.distance.text;
var duration = element.duration.text;
}
}
}})
//distance end
} else {
window.alert('Directions request failed due to ' + status);
}
})}
//XML Datadump
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD3ONxAJ8AoJ3uSUxRuYzKBzICvFj1reFA&libraries=places&callback=initMap">
</script>
</body>
</html>