-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmap.js
More file actions
78 lines (73 loc) · 2.17 KB
/
gmap.js
File metadata and controls
78 lines (73 loc) · 2.17 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
var map,
geocoder,
bounds = new google.maps.LatLngBounds(),
markersArray = [],
origin,
destination,
init = function(){
var opts = {
center: new google.maps.LatLng(65, 24),
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoom: 4
};
map = new google.maps.Map(document.getElementById('map'),opts);
geocoder = new google.maps.Geocoder();
},
calculateDistance = function(){
var service = new google.maps.DistanceMatrixService();
origin = document.getElementById('origin').value + ", Finland";
destination = document.getElementById('destination').value + ", Finland";
service.getDistanceMatrix({
origins : [origin],
destinations : [destination],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, callback);
},
callback = function(response, status){
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Virhe: ' + status);
} else {
var origins = response.originAddresses,
destinations = response.destinationAddresses,
outputDiv = document.getElementById('outputDiv'),
results;
outputDiv.innerHTML = '';
deleteOverlay();
for (var i = 0; i < origins.length; i++) {
results = response.rows[i].elements;
addMarker(origins[i]);
for (var j = 0; j < results.length; j++) {
outputDiv.innerHTML += origins[i] + " -> " + destinations[j]
+ ": " + results[j].distance.text + ". Aika "
+ results[j].duration.text + "<br />";
addMarker(destinations[i]);
}
}
}
},
addMarker = function(location) {
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
});
markersArray.push(marker);
} else {
alert("Geocode was not successful for the following reason: "+ status);
}
});
},
deleteOverlay = function(){
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
};