简体   繁体   English

地理位置错误检测

[英]Geolocation Error Detection

I'm using Geolocation but I'm having trouble recognizing errors to be able to offer an alternative. 我正在使用Geolocation,但是无法识别错误,因此无法提供替代方法。

My HTML looks like this: 我的HTML看起来像这样:

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

My script looks like this: 我的脚本如下所示:

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() {
});

My version of IE9 does not support Geolocation (I've tried their test site with their own script), but it gives me no error or warning, plus if I do not allow location in Firefox or Chrome, I don't get any error or alert either. 我的IE9版本不支持地理位置定位(我已经使用自己的脚本尝试了他们的测试站点),但是它没有错误或警告,而且如果我不允许在Firefox或Chrome中定位,也不会出现任何错误或提醒。

Can someone help? 有人可以帮忙吗? If it cannot run, i can offer an alternative so I don't think i need to look through error codes so much, but I do need to be able to detect failure so I can offer my alternative, but the error portion in my script will not run, regardless. 如果无法运行,我可以提供替代方案,因此我认为我不需要仔细检查错误代码,但是我确实需要能够检测到故障,因此我可以提供替代方案,但是脚本中的错误部分不会运行,无论如何。

My question really is, why won't this else run? 我的问题确实是,为什么不这样做?

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

Thanks 谢谢

For your error message it will appear if your browser does not support geolocation, to enable disable geolocation on browser that supports it you usually need to take look next to the address bar there should be a small icon that you can use to disable/enable geolocation. 对于您的错误消息,如果您的浏览器不支持地理定位,则会出现该错误消息,要在支持它的浏览器上启用禁用地理定位,通常需要在地址栏旁边看一下,您应该可以使用一个小图标来禁用/启用地理定位。

In order for you to detect previous decision weather you enabled/disabled geolocation you can use the solution I suggested in this article : 为了使您能够检测先前启用或禁用地理位置的决策天气,可以使用我在本文中建议的解决方案:

Is there a way of detecting whether a user has already given permission to use navigator.geolocation? 有没有一种方法可以检测用户是否已被授予使用navigator.geolocation的权限?

For IE9 it is working fine, the only thing is you should click Allow blocked cnotent at the bottom of the web page. 对于IE9,它工作正常,唯一的事情是您应该单击网页底部的“ Allow blocked cnotent点”。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM