-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgeofunctions.js
394 lines (335 loc) · 11 KB
/
geofunctions.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/**
* geofunctions.js
* Contains functions for getting geo coordinates and uses the api from geonames.org
*
* geonames.org international geocoder is using google geocoder,
* reverse geocoding is provided by geonames using openstreemap data
* (Feb 2014) under a cc by-sa license.
*
* Powered by geonames.org under a cc by license.
*
* Made for TOM Makeathon at Northwestern University
*
* @license MIT license
* @version 1.0
* @author Daniel Bednarczyk, Darcy Green (Need Knower), Joe Cummings, Julie Davies, Megan Reid, Wong Song Wei
* @updated 2017-05-16
* @link https://makeathon-nu.github.io/Street-Nav/
*
* If address or intersection aren't working:
* Be sure that the username is entered correctly, capitalization matters.
* GPS must be enabled for the app or web browser on the device.
* Try clearing the cache for the web browser.
* Allow the browser to know your location.
*
* Callback functions:
* OnGetGeoCoordinates(position)
* OnGetCurrentPositionError(positionError)
* OnWatchPositionError(positionError)
* OnGetCurrentLocation(response)
* OnGetNearestIntersection(response)
*
*/
// Id returned by setTimeout to regularly check for intersections.
var m_idFollowStreets = 0;
// Id returned by setTimeout to regularly check for addresses.
var m_idFollowAddress = 0;
// Username to use for geonames.org
var m_strUsername = "";
// Waiting for GeoNames to return with the current address, one request is outstanding if true.
// Don't want more than one outstanding call to it.
var m_bAddressWaiting = false;
// Waiting for GeoNames to return with the nearest intersection, one request is outstanding if true.
// Don't want more than one outstanding call to it.
var m_bIntersectionWaiting = false;
// Granularity for requesting coordinates when following streets in milliseconds.
// geonames.org allows 2000 credits an hour, which equates to one every 1.8 seconds.
var m_iIntersectionFrequencyMs = 3000;
// Granularity for requesting coordinates when following address in milliseconds.
var m_iAddrFrequencyMs = 10000;
// Use international api, non-United States
var m_bInternational = false;
// This is set to get the current location after getting the coordinates response.
var m_bGetCurrentLocation = false;
// This is set to get the nearest intersection after getting the coordinates response.
var m_bGetNearestIntersection = false;
// Holds the last coordinates given by watchPosition
var m_geoLastCoordinates = null;
// Id returned by watchPosition to regularly get the geolocation coordinates
var m_idWatchPosition = 0;
/*
Create OnGetGeoCoordinates(position) to receive the coordinates and act on them.
*/
function GetGeoCoordinates()
{
if(navigator.geolocation)
{
var date = new Date();
if(m_geoLastCoordinates
&& m_geoLastCoordinates.coords.latitude != 0
&& m_geoLastCoordinates.coords.longitude != 0
&& date.getTime() - m_geoLastCoordinates.timestamp <= 10000)
{
// Use the last coordinates, they are less than 10 seconds old.
OnUseCurrentPosition(m_geoLastCoordinates);
return;
}
// We don't have a coordinate in the last 10 seconds, try to get one now.
navigator.geolocation.getCurrentPosition(successGetCurrentPosition, errorGetCurrentPositionAccurate, {maximumAge: 20000, timeout:5000, enableHighAccuracy: true});
}
else
{
alert("Geolocation is not supported by this browser.");
}
}
/**
Success function for getCurrentPosition
*/
function successGetCurrentPosition(position)
{
// Use the coordinates returned.
OnUseCurrentPosition(position);
};
/**
Error function for getCurrentPosition when using enableHighAccuracy: false.
*/
function errorGetCurrentPositionLessAccurate(positionError)
{
// Pass the error onto the GUI
OnGetCurrentPositionError(positionError);
};
/**
Error function for getCurrentPosition when using enableHighAccuracy: true.
*/
function errorGetCurrentPositionAccurate(positionError)
{
// Try the less accurate getCurrentPosition.
navigator.geolocation.getCurrentPosition(successGetCurrentPosition, errorGetCurrentPositionLessAccurate, {maximumAge: 20000, timeout:5000, enableHighAccuracy: false});
}
/**
Success function for watchPosition
*/
function successWatchPosition(position)
{
// Save the last coordinates
m_geoLastCoordinates = position;
// See if there is anything to do with the coordinates
OnUseCurrentPosition(position);
};
/**
Error function for watchPosition when using enableHighAccuracy: true.
*/
function errorWatchPosition(positionError)
{
if(m_idWatchPosition != 0)
{
// Stop watching and start again
navigator.geolocation.clearWatch(m_idWatchPosition);
m_idWatchPosition = navigator.geolocataion.watchPosition(successWatchPosition, errorWatchPosition, {maximumAge: 20000, timeout:10000, enableHighAccuracy: true});
}
// Pass the error onto the GUI.
OnWatchPositionError(positionError);
};
/**
Operate on the coordinates given
*/
function OnUseCurrentPosition(position)
{
if(m_bGetCurrentLocation)
{
// Get the current address
GetCurrentLocation(position.coords.latitude, position.coords.longitude);
m_bGetCurrentLocation = false;
}
if(m_bGetNearestIntersection)
{
// Get the nearest intersection
GetNearestIntersection(position.coords.latitude, position.coords.longitude);
m_bGetNearestIntersection = false;
}
// Pass the coordinates onto the GUI.
OnGetGeoCoordinates(position);
};
/*
Get the current address.
If no coordinates are given, get coordinates.
*/
function GetCurrentLocation(dblLatitude, dblLongitude)
{
if(arguments.length == 0
|| (dblLatitude == 0 && dblLongitude == 0))
{
// Get coordinates and get the current location using them.
m_bGetCurrentLocation = true;
GetGeoCoordinates();
return;
}
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if(this.readyState == 4)
{
// Request is finished.
m_bAddressWaiting = false;
}
// Pass the current location information onto the GUI.
OnGetCurrentLocation(this);
};
if(m_bAddressWaiting)
{
// There is already an outstanding call for the current address
return;
}
// Request the current address.
m_bAddressWaiting = true;
xhttp.open("GET", "https://secure.geonames.org/findNearestAddressJSON?lat=" + dblLatitude.toString() + "&lng=" + dblLongitude.toString() + "&username=" + m_strUsername, true);
xhttp.send();
};
/*
Get the nearest intersection.
If no coordinates are given, get coordinates.
*/
function GetNearestIntersection(dblLatitude, dblLongitude)
{
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
// Pass the nearest intersection information onto the GUI.
if(this.readyState == 4)
{
// Request is finished.
m_bIntersectionWaiting = false;
}
OnGetNearestIntersection(this);
};
if(m_bIntersectionWaiting)
{
// There is already an outstanding call for the nearest intersection
return;
}
// Request the nearest intersection
m_bIntersectionWaiting = true;
if(m_bInternational)
{
xhttp.open("GET", "https://secure.geonames.org/findNearestIntersectionOSMJSON?lat=" + dblLatitude.toString() + "&lng=" + dblLongitude.toString() + "&username=" + m_strUsername, true);
}
else
{
xhttp.open("GET", "https://secure.geonames.org/findNearestIntersectionJSON?lat=" + dblLatitude.toString() + "&lng=" + dblLongitude.toString() + "&username=" + m_strUsername, true);
}
xhttp.send();
};
/**
Start following the street intersections
*/
function StartFollowingStreets()
{
if(m_idFollowStreets != 0)
{
// Already following street intersections
return;
}
// Start a timer for intersections
m_idFollowStreets = setInterval(OnFollowStreetsTimer, m_iIntersectionFrequencyMs);
if(m_iAddrFrequencyMs != 0)
{
// Start a timer for the address
m_idFollowAddress = setInterval(OnFollowAddressTimer, m_iAddrFrequencyMs);
}
if(m_idWatchPosition == 0)
{
// Start getting coordinates regularly
m_idWatchPosition = navigator.geolocation.watchPosition(successWatchPosition, errorWatchPosition, {maximumAge: 20000, timeout:10000, enableHighAccuracy: true});
}
if(m_iAddrFrequencyMs != 0)
{
// Set to get the address
m_bGetCurrentLocation = true;
}
// Set to get the intersection
m_bGetNearestIntersection = true;
// Get the first coordinates
GetGeoCoordinates();
};
/**
Stop following the street intersections
*/
function StopFollowingStreets()
{
// Clear address and intersection timers
clearTimeout(m_idFollowStreets);
clearTimeout(m_idFollowAddress);
// Stop getting coordinates
navigator.geolocation.clearWatch(m_idWatchPosition);
// Reset all id's and variables
m_idFollowStreets = 0;
m_idFollowAddress = 0;
m_idWatchPosition = 0;
m_bGetNearestIntersection = false;
m_bGetCurrentLocation = false;
// Reset last coordinates
m_geoLastCoordinates = null;
};
/**
Timer function for street intersections
*/
function OnFollowStreetsTimer()
{
// Set to get the intersection
m_bGetNearestIntersection = true;
if(m_bIntersectionWaiting
|| m_idFollowStreets == 0)
{
// Don't put another request in or we no longer want it.
return;
}
// Use current coordinates or get new ones
GetGeoCoordinates();
};
/**
Timer function for current address
*/
function OnFollowAddressTimer()
{
// Set to get the address
m_bGetCurrentLocation = true;
if(m_bAddressWaiting
|| m_idFollowAddress == 0)
{
// Don't put another request in or we no longer want it.
return;
}
// Use current coordinates or get new ones
GetGeoCoordinates();
};
/**
Get the bearing from one coordinate to another
Formula: θ = atan2( sin Δλ ⋅ cos φ2 , cos φ1 ⋅ sin φ2 − sin φ1 ⋅ cos φ2 ⋅ cos Δλ )
where φ1,λ1 is the start point, φ2,λ2 the end point (Δλ is the difference in longitude)
*/
function GetBearing(fromPosition, toPosition)
{
if(!fromPosition || !toPosition)
{
return;
}
var y = Math.sin(toRadians(toPosition.coords.longitude - fromPosition.coords.longitude)) * Math.cos(toRadians(toPosition.coords.latitude));
var x = Math.cos(toRadians(fromPosition.coords.latitude)) * Math.sin(toRadians(toPosition.coords.latitude)) - Math.sin(toRadians(fromPosition.coords.latitude)) *
Math.cos(toRadians(toPosition.coords.latitude))*Math.cos(toRadians(toPosition.coords.longitude - fromPosition.coords.longitude));
var dblBearing = Math.atan2(y, x);
return toDegrees(dblBearing);
};
/**
Convert Radians to Degrees
*/
function toDegrees(angle)
{
return angle * (180 / Math.PI);
};
/**
Convert Degrees to Radians
*/
function toRadians(angle)
{
return angle * (Math.PI / 180);
};