最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a way to know if geolocation is enabled for the current domain? - Stack Overflow

programmeradmin1浏览0评论

The website I'm working on uses HTML5 geolocation api. It works fine, but I need to know if the feature has been authorized.
navigator.geolocation only tells if the feature is available. What I want to know is whether the user has enabled it already. Basically, I need to know if the browser will request permission to use the feature. Is it possible?

The website I'm working on uses HTML5 geolocation api. It works fine, but I need to know if the feature has been authorized.
navigator.geolocation only tells if the feature is available. What I want to know is whether the user has enabled it already. Basically, I need to know if the browser will request permission to use the feature. Is it possible?

Share Improve this question asked Aug 20, 2013 at 14:36 AntoineAntoine 5,25512 gold badges56 silver badges88 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

If the user does not allow your app to use the Geolocation API the methods getCurrentPosition and watchPosition will return an PositionError object via the error callback:

void getCurrentPosition(successCallback,
                        errorCallback,
                        options);

long watchPosition(successCallback,
                   errorCallback,
                   options);

The PositionError looks like:

PositionError {
    code;
    message;
};

where the possibles values of code are:

PositionError.PERMISSION_DENIED = 1;
PositionError.POSITION_UNAVAILABLE = 2;
PositionError.TIMEOUT = 3;

And more specific:

  • PERMISSION_DENIED (numeric value 1) The location acquisition process failed because the document does not have permission to use the Geolocation API.

The documentation here.

There is no check for permanent authorization.
The only thing you can do imho is try to "guess" if the user has authorized by estimate time.
For example:

  • more than 1,5 seconds to retrieve geolocation -> probably user got to click interface and give auth

  • less than 500ms -> pretty sure user already give auth

发布评论

评论列表(0)

  1. 暂无评论