14
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Geolocation APIをjQuery.Deferredでラップ

Posted at

Geolocation APIをjQuery.Deferredでラップ

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が未対応
        }
    });
14
14
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?