Created
July 1, 2015 14:56
-
-
Save yerffejytnac/81e5cad12abcc63324b7 to your computer and use it in GitHub Desktop.
Geolocation Service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
var geolocation; | |
if ('geolocation' in navigator) { | |
geolocation = navigator.geolocation; | |
} | |
export default Ember.Service.extend({ | |
maximumAge: 30000, | |
timeout: 27000, | |
enableHighAccuracy: true, | |
start: function () { | |
if (!geolocation) { | |
throw new Error("Geolocation is not available for this device."); | |
} | |
// We're already watching the position | |
if (this.watchID) { | |
return; | |
} | |
this.watchID = geolocation.watchPosition(position => { | |
var currentPosition = this.get('currentPosition'); | |
if (!currentPosition || position.timestamp > currentPosition.timestamp) { | |
this.set('currentPosition', position); | |
} | |
}); | |
}, | |
getPosition: function () { | |
this.start(); | |
var options = buildOptions(this); | |
return new Ember.RSVP.Promise(function (resolve, reject) { | |
geolocation.getCurrentPosition(resolve, reject, options); | |
}); | |
} | |
}); | |
function buildOptions(service) { | |
return service.getProperties('maximumAge', 'timeout', 'enableHighAccuracy'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment