Skip to content

Instantly share code, notes, and snippets.

@foo9
Last active August 29, 2015 13:57
Show Gist options
  • Save foo9/9458096 to your computer and use it in GitHub Desktop.
Save foo9/9458096 to your computer and use it in GitHub Desktop.
Geolocation API
function geolocation() {
var deferred = $.Deferred();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
deferred.resolve(position.coords);
}, function (error) {
deferred.reject(error.code);
});
} else {
deferred.reject();
}
return deferred.promise();
}
geolocation()
.done(function(coords) {
console.log(coords);
})
.fail(function(errorCode) {
switch (errorCode) {
case 1:
// 位置情報の利用が許可されていません
break;
case 2:
// デバイスの位置が判定できません
break;
case 3:
// タイムアウト
break;
default:
// APIが未対応
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment