gpt4 book ai didi

javascript - 捕获 html5 地理定位事件

转载 作者:行者123 更新时间:2023-12-03 09:36:33 25 4
gpt4 key购买 nike

有没有办法捕获 html5 地理定位事件?

现在我正在使用 geolocator.js

我的问题是:

当消息弹出时,如果我想允许 html5 位置,如果我接受,则如果我获取 ipLocation,则需要 html5 位置。

但我想监听这些点击,这样我就可以设置一个超时,如果用户在 10 秒内没有点击,它会自动获取 IP 位置。

有什么办法可以获取“接受”和“拒绝”的事件吗?这是我的代码片段

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(geoSuccess, geoError, html5Options);
} else { // not supported
fallback(new Error('geolocation is not supported.'));
}

编辑:我在 iMac 上使用 Chrome

编辑2:

var html5Options = { enableHighAccuracy: true,timeout: 10000, maximumAge: 0 };

编辑3:使用kravitz的解决方案

if (navigator.geolocation) {
waitTooLong=setTimeout(geoError, 15000);
navigator.geolocation.getCurrentPosition(geoSuccess, geoError, html5Options);
} else { // not supported
fallback(new Error('geolocation is not supported.'));
}

function geoSuccess(position) {
clearTimeout(waitTooLong);
//doing my locationstuff
}

function geoError(error) {
//doing my locationstuff
clearTimeout(waitTooLong);
fallback(error);
}

最佳答案

var geoError = function(){
//get location failed so get manually by IP here
};

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(geoSuccess, geoError, html5Options);
} else { // not supported
fallback(new Error('geolocation is not supported.'));
}

以上应该可行,但如果你想确定:

var timeOut = setTimeout(geoError, 10000);
var geoSuccess = function(){
clearTimeout(timeOut);
};

关于javascript - 捕获 html5 地理定位事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31318166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com