-
Notifications
You must be signed in to change notification settings - Fork 1
/
burritoFinderScripts.js
228 lines (181 loc) · 6.53 KB
/
burritoFinderScripts.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* Created By: Adrienne Cabouet
* Date: 7/9/13
* Time: 2:19 PM
*/
var ourCoords = {
latitude: 45.5047947,
longitude: -122.6534852
};
var map;
var watchID = null;
var options = {enableHighAccuracy: true, timeout: 100, maximumAge: 0};
var prevCoords = null;
var geocoder;
var currentPosition;
var directionsService;
var directionsDisplay;
var km;
window.onload = getMyLocation;
function getMyLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
displayLocation,
displayError,
options);
var watchButton = document.getElementById("watch");
watchButton.onclick = watchLocation;
var clearWatchButton = document.getElementById("clearWatch");
clearWatchButton.onclick = clearWatch;
var directionsButton = document.getElementById("getDirections");
directionsButton.onclick = getDirections;
var convertButton = document.getElementById("convert");
convertButton.onclick = convert;
} else {
alert("Oops, your browser doesn't support geo-location.");
}
}
function displayLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
currentPosition = new google.maps.LatLng(latitude, longitude);
directionsService = new google.maps.DirectionsService();
var div = document.getElementById("location");
div.innerHTML = "You are at Latitude: " + latitude + ", Longitude: " + longitude;
div.innerHTML += "<p>" + "Within " + position.coords.accuracy + " meters accuracy." + "</p>";
div.innerHTML += "Found in " + options.timeout + " milliseconds.";
km = computeDistance(position.coords, ourCoords);
var miles = Math.floor(km * 0.6214);
var distance = document.getElementById("distance");
if (miles > 1) {
distance.innerHTML = "You're " + miles + " miles from delicious burritos.";
} else {
distance.style.display = "none";
}
if (map == null) {
showMap(position.coords);
prevCoords = position.coords;
} else {
var meters = computeDistance(position.coords, prevCoords) * 1000;
if (meters > 20) {
scrollMapToPosition(position.coords);
prevCoords = position.coords;
}
}
}
function displayError(error) {
var errorTypes = {
0: "Unknown error",
1: "Permission denied by user",
2: "Position is not available",
3: "Request time out"
};
var errorMessage = errorTypes[error.code];
if (error.code == 0 || error.code == 2) {
errorMessage = errorMessage + " " + error.message;
}
var div = document.getElementById("location");
div.innerHTML = errorMessage;
options.timeout += 100;
navigator.geolocation.getCurrentPosition(
displayLocation,
displayError,
options);
div.innerHTML += " . . . checking again with timeout=" + options.timeout;
}
function computeDistance(startCoords, destCoords) {
var startLatRads = degreesToRadians(startCoords.latitude);
var startLongRads = degreesToRadians(startCoords.longitude);
var destLatRads = degreesToRadians(destCoords.latitude);
var destLongRads = degreesToRadians(destCoords.longitude);
var Radius = 6371; //radius of the Earth in km
var distance = Math.acos(Math.sin(startLatRads) * Math.sin(destLatRads) +
Math.cos(startLatRads) * Math.cos(destLatRads) *
Math.cos(startLongRads - destLongRads)) * Radius;
return distance;
}
function degreesToRadians(degrees) {
var radians = (degrees * Math.PI)/180;
return radians;
}
function showMap(coords) {
var googleLatAndLong = new google.maps.LatLng(coords.latitude, coords.longitude);
var mapOptions = {
zoom: 15,
center: googleLatAndLong,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var mapDiv = document.getElementById("map");
map = new google.maps.Map(mapDiv, mapOptions);
var title ="Your Location";
var content = "You are here: " + coords.latitude + ", " + coords.longitude;
addMarker(map, googleLatAndLong, title, content);
}
function addMarker(map, latlong, title, content) {
var markerOptions = {
position: latlong,
map: map,
title: title,
clickable: true
};
var marker = new google.maps.Marker(markerOptions);
var infoWindowOptions = {
content: content,
position: latlong
};
var infoWindow = new google.maps.InfoWindow(infoWindowOptions);
google.maps.event.addListener(marker,"click", function() {
infoWindow.open(map);
});
}
function watchLocation() {
watchID = navigator.geolocation.watchPosition(displayLocation,
displayError,
options);
}
function clearWatch() {
if(watchID) {
navigator.geolocation.clearWatch(watchID);
watchID = null;
}
}
function scrollMapToPosition(coords) {
var latitude = coords.latitude;
var longitude = coords.longitude;
var latlong = new google.maps.LatLng(latitude, longitude);
map.panTo(latlong);
addMarker(map, latlong, "Your new location", "You moved to: " + latitude + ", " + longitude);
}
function convert() {
geocoder = new google.maps.Geocoder;
geocoder.geocode({"latLng" : currentPosition}, function(results, status){
var div = document.getElementById("location");
div.innerHTML = "You are at " + (results[0].formatted_address);
});
}
function getDirections() {
var burritos = new google.maps.LatLng(45.5047947, -122.6534852);
var request = {
origin: currentPosition,
destination: burritos,
travelMode: google.maps.TravelMode.BICYCLING
};
var directionsPanel = document.createElement("div");
directionsPanel.setAttribute("id", "directionsPanel");
var oldDirectionsPanel = document.getElementById("directionsPanel");
var body = document.getElementsByTagName("body")[0];
if (oldDirectionsPanel == null) {
body.appendChild(directionsPanel);
} else {
body.replaceChild(directionsPanel, oldDirectionsPanel);
}
if (km > 0) {
directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setPanel(directionsPanel);
directionsDisplay.setMap(map);
directionsService.route(request, function(response, status) {
if(status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});}
}