Last active
August 29, 2015 13:57
-
-
Save foo9/9458096 to your computer and use it in GitHub Desktop.
Geolocation API
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
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