66557023caee54cb0071f8d5a78046f5508b615f
[maps-routes-comparison] / googlemaps.js
1 // Copyright (c) Guillaume Cottenceau and licensed under the Apache 2.0 License
2
3 function debug(txt) {
4     document.getElementById("debugbox").innerHTML += new Date() + txt + "<br/>";
5 }
6
7 function downloadUrl(url, callback) {
8     var request = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;
9     request.onreadystatechange = function() {
10         if (request.readyState == 4) {
11             request.onreadystatechange = function() {};
12             if (callback) {
13                 callback(request.responseText, request.status);
14             }
15         }
16     };
17     request.open('GET', url, true);
18     request.send(null);
19 }
20
21 function getIcon(filename) {
22     return new google.maps.MarkerImage("icons/" + filename + ".png",
23                                        new google.maps.Size(32, 32),
24                                        new google.maps.Point(0, 0),
25                                        new google.maps.Point(16, 32));
26 }
27 function getSelectedIcon(filename) {
28     return new google.maps.MarkerImage("icons/" + filename + "h.png",
29                                        new google.maps.Size(32, 32),
30                                        new google.maps.Point(0, 0),
31                                        new google.maps.Point(16, 32));
32 }
33
34 function getShadow(filename) {
35     return new google.maps.MarkerImage("icons/" + filename + "s.png",
36                                        new google.maps.Size(56, 32),
37                                        new google.maps.Point(0, 0),
38                                        new google.maps.Point(16, 32));
39 }
40
41 function getNameRegion(name, region) {
42     return region.length > 0 ? name + ", " + region : name;
43 }
44
45
46 function displayDirectionsMoreinfo(route, node) {
47     var total_distance = 0, total_duration = 0;
48     for (i = 0; i < route.legs.length; i++) {
49         total_distance += route.legs[i].distance.value;
50         total_duration += route.legs[i].duration.value;
51     }
52     total_distance = Math.round(total_distance / 1000.0);
53     var total_hours = parseInt(total_duration / 60.0 / 60.0);
54     var total_minutes = Math.round(total_duration / 60.0 - total_hours * 60);
55     var base_sentence = total_hours == 0 ? '{1} km, about {2} minutes'
56                                          : '{1} km, about {3} hours and {2} minutes';
57     node.innerHTML = base_sentence.replace('{1}', total_distance)
58                                   .replace('{2}', total_minutes)
59                                   .replace('{3}', total_hours);
60 }