Skip to content

Instantly share code, notes, and snippets.

@yerffejytnac
Created July 1, 2015 14:56
Show Gist options
  • Save yerffejytnac/81e5cad12abcc63324b7 to your computer and use it in GitHub Desktop.
Save yerffejytnac/81e5cad12abcc63324b7 to your computer and use it in GitHub Desktop.
Geolocation Service
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