Last active
August 29, 2015 14:06
-
-
Save dstaley/3d2befde5b7a1f0fd4b9 to your computer and use it in GitHub Desktop.
This file contains 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
export default { | |
name: 'geolocation-service', | |
initialize: function(container, app) { | |
app.inject('route', 'geolocationService', 'service:geolocation'); | |
app.inject('controller', 'geolocationService', 'service:geolocation'); | |
} | |
}; |
This file contains 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'; | |
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