the*_*rld 47 javascript firefox callback geolocation
所以我有这个javascript代码.在safari和chrome中,如果用户拒绝共享位置,它将失败,因为它应该; 但是,在Firefox中,它没有.
任何帮助赞赏.
function initGeolocation()
{
if( navigator.geolocation )
{
// Call getCurrentPosition with success and failure callbacks
navigator.geolocation.getCurrentPosition( success, fail );
}
else
{
alert("Sorry, your browser does not support geolocation services.");
}
}
var map;
function success(position)
{
var longIDText = document.getElementById('longID');
var latIDText = document.getElementById('latID');
longIDText.value = position.coords.longitude;
latIDText.value = position.coords.latitude;
document.getElementById('coordSubmitID').click();
}
function fail(error)
{
alert("FAAAAAAAAAAIIIIIIIIIL")
var zip_code ;
while (true){
// Could not obtain location
zip_code = prompt("Please enter your current address or zip code","");
if ( zip_code == "" ) {
alert(zip_code +" is not a valid address. Please try again.");
}
else{
break;
}
}
var zipIDText = document.getElementById('zipID');
zipIDText.value = zip_code;
document.getElementById('coordSubmitID').click();
}
Run Code Online (Sandbox Code Playgroud)
Ale*_* K. 29
对于Firefox,似乎 只有在选择"永不共享"时才会PERMISSION_DENIED引发; 如果对话框被解除或选择"不是现在",实际上没有任何反应 - 即使在mozillas地理定位演示中,如果您解除权限UI没有任何反应.
这意味着getCurrentPosition可以返回,因为用户关闭了确认UI,或者因为它成功启动了异步请求 - 似乎没有办法区分这两者.
https://bugzilla.mozilla.org/show_bug.cgi?id=675533
小智 21
这是一个真正的痛苦,绝对不是理想的功能.
我用来保存等待的用户的解决方法是设置超时以检查等待微调器是否在3秒后显示,如果是,则隐藏它并显示手动邮政编码输入:
var latlng;
var waitTime = 3000;
try {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
success(position);
}, showError);
} else {
showError("NOT-SUPPORTED");
}
var t = setTimeout(function () {
if ($("#getZip div.loading").css("display") != "none") {
$("#getZip div.loading").hide();
$("#errorZip").show();
}
}, waitTime);
} catch (evt) {
alert(evt);
}
Run Code Online (Sandbox Code Playgroud)
GFo*_*y83 13
正如alexleonard指出的那样,我发现的唯一可靠的跨浏览器解决方案是setTimeout()检查latLng对象/变量的状态,因为timeout选项getCurrentPosition()似乎不可靠.以下示例在IE11,Chrome33和Firefox28中进行了测试.
有关使用jQuery承诺的更完整的解决方案,请查看我的要点:https://gist.github.com/GFoley83/10092929
示例 - 点击F12,粘贴到控制台并运行
var latLng,
geoOptions = {
enableHighAccuracy: false,
timeout: 5000, // Wait 5 seconds
maximumAge: 300000 // Valid for 5 minutes
};
var userLocationFound = function(position){
latLng = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
window.console.log("User confirmed! Location found: " + latLng.lat + ", " + latLng .lng);
}
var userLocationNotFound = function(){
latLng = {
lat: -41.29247, // fallback lat
lng: 174.7732 // fallback lng
};
window.console.log("Fallback set: ", latLng);
}
window.navigator.geolocation.getCurrentPosition(userLocationFound, userLocationNotFound, geoOptions);
setTimeout(function () {
if(!latLng){
window.console.log("No confirmation from user, using fallback");
userLocationNotFound();
}else{
window.console.log("Location was set");
}
}, geoOptions.timeout + 1000); // Wait extra second
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23856 次 |
| 最近记录: |