-
Notifications
You must be signed in to change notification settings - Fork 1
/
index (Jose3).html~
153 lines (132 loc) · 4.79 KB
/
index (Jose3).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
<!DOCTYPE html>
<html>
<head>
<title>Google Maps API v3 Example: Distance Matrix</title>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=true"></script>
<style>
body {
margin: 20px;
font-family: courier, sans-serif;
font-size: 12px;
}
#map {
height: 480px;
width: 640px;
border: solid thin #333;
margin-top: 20px;
}
</style>
<script>
durationLimiter = secConverter(40);
var map;
var geocoder;
var bounds = new google.maps.LatLngBounds();
var markersArray = [];
origin = 'Royal School of Mines, London';
var destinationA = 'Buckingham Palace, London, England';
var destinationB = 'Trafalgar Square, London, England';
var destinationC = 'Picadilly Circus, London, England';
var destinationD = 'Hyde Park, London, England';
var destinationE = 'Covent Garden, London, England';
var destinationIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=D|FF0000|000000';
var originIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=O|FFFF00|000000';
function initialize() {
var opts = {
center: new google.maps.LatLng(55.53, 9.4),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'), opts);
geocoder = new google.maps.Geocoder();
}
function printArea() {
var myString = '<p><img src="http://maps.googleapis.com/maps/api/staticmap?center=' + origin + '&zoom=11&size=200x200&sensor=false"></p>';
document.write(myString);
document.write('<button type="button" onclick="window.location.reload();">return</button></p>');
}
function calculateDistances() {
origin=prompt("please enter your location","");
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin],
destinations: [destinationA, destinationB, destinationC, destinationD, destinationE],
travelMode: google.maps.TravelMode.WALKING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callbackDuration);
}
function callbackDuration(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
} else {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var outputDiv = document.getElementById('outputDiv');
outputDiv.innerHTML = '';
deleteOverlays();
for (var i = 0; i < origins.length; i++) {
var results = response.rows[i].elements;
addMarker(origins[i], false);
for (var j = 0; j < results.length; j++) {
if(results[j].duration.value<durationLimiter){
addMarker(destinations[j], true);
outputDiv.innerHTML += origins[i] + ' to ' + destinations[j]
+ ': ' + results[j].distance.text + ' in '
+ results[j].duration.text + '<br>';
}
}
}
}
}
function secConverter(mins){// time is in mins
return (mins*60);
}
function addMarker(location, isDestination) {
var icon;
if (isDestination) {
icon = destinationIcon;
} else {
icon = originIcon;
}
geocoder.geocode({'address': location}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
bounds.extend(results[0].geometry.location);
map.fitBounds(bounds);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
icon: icon
});
markersArray.push(marker);
} else {
alert('Geocode was not successful for the following reason: '
+ status);
}
});
}
function deleteOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
}
</script>
</head>
<body onload="initialize()">
```
<div id="inputs">
<pre class="prettyprint">
</pre>
<p><button type="button" onclick="calculateDistances();">Calculate
distances</button></p>
<p><button type="button" onclick="printArea();">Print area</button></p>
</div>
<div id="outputDiv"></div>
<div id="map"></div>
</body>
</html>