forked from g33kism/gmaps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistanceFromTo.html
More file actions
64 lines (61 loc) · 2.15 KB
/
DistanceFromTo.html
File metadata and controls
64 lines (61 loc) · 2.15 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
<!DOCTYPE html>
<html>
<!--refer https://developers.google.com/maps/documentation/javascript/examples/distance-matrix -->
<head>
<title>Distance Matrix service</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div id="right-panel">
From:
<input type="text" id="txtFrom" />
To:
<input type="text" id="txtTo" />
<button id="distance">Calculate Distance</button>
<div>
<strong>Results</strong>
</div>
<div id="output"></div>
</div>
<script>
//created as Global just to debug in console
var origin;
var destination;
var results;
$("#distance").click(function () {
origin = document.getElementById('txtFrom').value;
destination = document.getElementById('txtTo').value;
var geocoder = new google.maps.Geocoder;
var service = new google.maps.DistanceMatrixService;
//getting distance from Google
service.getDistanceMatrix({
origins: [origin],
destinations: [destination],
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;
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = '';
for (var i = 0; i < originList.length; i++) {
results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
outputDiv.innerHTML += originList[i] + ' to ' + destinationList[j] +
': ' + results[j].distance.text + ' in ' +
results[j].duration.text + '<br>';
}
}
}
});
});
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAMxdGmGa60mA7EV-iPYauPrj9XB35qA6Q">
</script>
</body>
</html>