-
Notifications
You must be signed in to change notification settings - Fork 260
/
geocoder.coffee
73 lines (56 loc) · 1.97 KB
/
geocoder.coffee
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
# TODO: Update API To NOT USE GOOGLE and use Open Source
class Refuge.Library.Geocoder
statusOK: google.maps.GeocoderStatus.OK
googleGeocoder: new google.maps.Geocoder()
geocodeSearchString: (address) =>
_googleGeocoder = new google.maps.Geocoder()
# This function takes in a string and geocodes it, returning two coordinates.
# It will resolve a promise with an object that contains the
# latitude and longtitude.
requestBody = { 'address': address }
promise = $.Deferred()
success = (results, status) =>
if (status == @statusOK)
coords =
lat: results[0].geometry.location.lat(),
long: results[0].geometry.location.lng()
promise.resolve(coords)
else
promise.reject(status)
fail = (err) ->
promise.rejext(err)
_googleGeocoder.geocode(requestBody, success, fail)
return promise.promise()
getAddress: (coords) =>
requestBody = {'location': new google.maps.LatLng(coords.lat, coords.long)}
promise = $.Deferred()
console.log requestBody
success = (results, status) =>
if status == @statusOK
promise.resolve(results)
fail = (err) ->
promise.fail(err)
@googleGeocoder.geocode(requestBody, success, fail)
return promise.promise()
getCurrentLocation: ->
# This function returns a promise that evaluates to an object with the
# latitude and longitute of the current coordinates.
support = navigator.geolocation? && navigator.geolocation
options =
enableHighAccuracy: true
timeout: 5000
promise = $.Deferred()
# Callback functions
success = (pos) ->
result =
lat: pos.coords.latitude
long: pos.coords.longitude
promise.resolve(result)
error = (err) ->
promise.reject(err)
if support
navigator.geolocation.getCurrentPosition( success, error, options )
else
# Geolocation not supported
promise.reject("Not Supported")
return promise.promise()