-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.js
123 lines (110 loc) · 3.59 KB
/
Main.js
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
var map;
var marker = false; //is there an active marker on the screen?
var directionsService = null;
var dest = null;
var commuteTime = "0";
var currentOriginLocation = null;
/**
* Called from html file
* Creates a new instance for the map API and directions API.
*/
function initMap() {
//create an instance of the map
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 45.4085, lng: -75.700},
zoom: 13
});
//create an instance of the directions API
directionsService = new google.maps.DirectionsService();
//Listen for any clicks on the map.
google.maps.event.addListener(map, 'click', function(event) {
//Get the location of where the user clicked
var destLocation = event.latLng;
//If there is no marker on the screen already
if(marker === false){
//Create the marker.
marker = new google.maps.Marker({
position: destLocation,
map: map,
draggable: true //make it draggable
});
//drag listener for marker
google.maps.event.addListener(marker, 'dragend', function(event){
markerLocation();
});
} else{
//Marker has already been added, so just change its location.
marker.setPosition(destLocation);
}
//display and save the marker's coordinates
markerLocation();
});
}
/**
* Called every time the marker's position changes. Display coordinates on the screen.
*/
function markerLocation(){
//Get location.
dest = marker.getPosition();
//display the marker's coordinates on the screen
document.getElementById('lat').value = dest.lat(); //latitude
document.getElementById('lng').value = dest.lng(); //longitude
}
/**
* When the calculate button is pressed
* Calculates time to destination
*/
function calcRoute() {
var travelMode = document.getElementById("travelMode").value;
if (!dest){
return;
}
//loop through the map coordinates in the given area
for (var lat = 45.41; lat < 45.43; lat+=0.005){
for (var long = -75.7; long < -75.65; long+=0.005) {
currentOriginLocation = new google.maps.LatLng(lat, long);
//directions request
var request = {
origin: currentOriginLocation,
destination: dest,
travelMode: travelMode
};
directionsService.route(request, function (result, status) {
console.log(status);
//direction results returned successfully
if (status === 'OK') {
commuteTime = result.routes[0].legs[0].duration.text;
addMarker(result.request.origin.location, commuteTime);
}
//API was called too many times in 1 second
else if (status==='OVER_QUERY_LIMIT'){
sleep(2000); //wait until allowed to make more API calls
}
});
}
}
}
/**
* Adds a marker to the map
* @param location Google LatLng location object
* @param label Text to be displayed next to marker
*/
function addMarker(location, label) {
new google.maps.Marker({
position: location,
label: label,
map: map
});
}
/**
* Freezes all processes
* @param milliseconds How long to freeze for
*/
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}