Skip to content

Instantly share code, notes, and snippets.

@hems
Created June 28, 2015 04:03
Show Gist options
  • Save hems/52823bf5a63d842abfee to your computer and use it in GitHub Desktop.
Save hems/52823bf5a63d842abfee to your computer and use it in GitHub Desktop.
Simple "hand made" meteor geolocation API
retring = false
# make it global
@geolocation = new EventEmitter()
@geolocation.current = null
@geolocation.start = ->
if not navigator.geolocation
geolocation.emit 'error', code: -1
return
# shows a top bar for the sake of moving while waiting
NProgress.start() if not retring
on_success = ( location ) ->
NProgress.done() if not retring
retring = false
# compatibility
location =
lat: location.coords.latitude
lng: location.coords.longitude
if geolocation.current
c = latitude: geolocation.current.lat, longitude: geolocation.current.lng
p = latitude: location.lat, longitude: location.lng
distance = geolib.getDistance c, p
# distance didn't change more than 500 meters, no need to update atm
if distance < 500 then return
geolocation.emit 'position', location
geolocation.current = location
on_error = ( error ) ->
NProgress.done()
geolocation.emit 'error', error
retring = true
retry = -> if retring then geolocation.start()
# automatically retry in 1 seconds
setTimeout retry, 5000
navigator.geolocation.watchPosition( on_success, on_error, settings.gps.options )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment