简体   繁体   中英

Geolocation Error Detection

I'm using Geolocation but I'm having trouble recognizing errors to be able to offer an alternative.

My HTML looks like this:

<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.

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?

For IE9 it is working fine, the only thing is you should click Allow blocked cnotent at the bottom of the web page.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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