Skip to content

Instantly share code, notes, and snippets.

@dstaley
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dstaley/3d2befde5b7a1f0fd4b9 to your computer and use it in GitHub Desktop.
Save dstaley/3d2befde5b7a1f0fd4b9 to your computer and use it in GitHub Desktop.
export default {
name: 'geolocation-service',
initialize: function(container, app) {
app.inject('route', 'geolocationService', 'service:geolocation');
app.inject('controller', 'geolocationService', 'service:geolocation');
}
};
import Ember from 'ember';
export default Ember.Object.extend({
hasLocation: false,
location: null,
init: function() { return this.get('getPosition'); },
getPosition: function() {
var service = this;
return new Ember.RSVP.Promise(function(resolve, reject){
if (service.get('supportsGeolocation')) {
if (service.get('hasLocation')) {
resolve(service.get('location'));
} else {
navigator.geolocation.watchPosition(function(position) {
service.set('location', position);
service.set('hasLocation', true);
resolve(position);
}, function(error) {
reject(error);
});
}
} else {
reject(new Error("Browser does not support geolocation."));
}
});
},
supportsGeolocation: function() {
return 'geolocation' in window.navigator;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment