2013-07-18 78 views
0

我正在使用地理位置,但我在识别错误​​时遇到了问题,无法提供替代方案。地理位置错误检测

我的HTML看起来像这样:

<button onclick="getLocation()">Get your location now</button> 
<div id="google_canvas"></div> 
<p id = 'error'></p> 

我的剧本是这样的:

function getLocation(){ 
    if(navigator.geolocation) 
    { 
    var map; 
    var mapOptions = 
    { 
     zoom: 13, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }   
    map = new google.maps.Map(document.getElementById('google_canvas'), mapOptions); 
    navigator.geolocation.getCurrentPosition(function(position) 
    { 
     var geolocate = new google.maps.LatLng(position.coords.latitude, position.coords.longitude),latitude=document.getElementById("latitude"),longitude=document.getElementById("longitude"); 
     var infowindow = new google.maps.InfoWindow(
     { 
     map: map, 
     position: geolocate, 
     content: 
     ' * output content within map * ' 
     }); 

     map.setCenter(geolocate); 
     latitude.value = position.coords.latitude; 
     longitude.value = position.coords.longitude; 
    });   
    } 

    else 
    { 
    document.getElementById('error').innerHTML = 'No Geolocation Support'; 
    } 

}; 
google.maps.event.addListener(map, 'idle', function() { 
}); 

我IE9的版本不支持地理位置(我试过他们的测试现场用自己的脚本),但它不会给我任何错误或警告,再加上如果我不允许在Firefox或Chrome中定位,我也不会收到任何错误或警告。

有人可以帮忙吗?如果它无法运行,我可以提供一个替代方案,所以我不认为我需要仔细查看错误代码,但我确实需要能够检测到故障,因此我可以提供替代方案,但脚本中的错误部分无论如何不会运行。

我的问题确实是,为什么不能运行?

else 
{ 
document.getElementById('error').innerHTML = 'No Geolocation Support'; 
} 

感谢

回答

0

因为如果你的浏览器不支持地理定位,以使在浏览器中禁用地理位置,支持它,你通常需要采取旁边的地址栏看起来应该会出现在您的错误信息一个可以用来禁用/启用地理位置的小图标。

为了让你能侦测到先前的决定天气,你启用/禁用的地理位置,你可以使用我在这篇文章中提出的解决方案:

Is there a way of detecting whether a user has already given permission to use navigator.geolocation?

对于IE9它工作正常,唯一的一点是你应该点击网页底部的Allow blocked cnotent

enter image description here

+0

我没有得到错误信息,但 - 如果我这样做,这将是罚款 – user2246804

+0

在IE9看一看底部,然后点击'允许阻止脚本...' –

+0

对不起迈赫迪我没有得到那个错误,我承诺 – user2246804