check if location setting has been turned off in users browser

前端 未结 2 1347
悲哀的现实
悲哀的现实 2020-12-05 05:11

I would like to hide() or show() a button that allows users to use their current location based on whether or not they are currently allowing location to be used in their br

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 06:01

    Have you read http://www.w3schools.com/html/html5_geolocation.asp

    What you want to do is check the errors to see if they allowed it or denied the request.

    function getLocation() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition,showError);
      } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
      }
    }
    
    function showPosition(position) {
      x.innerHTML = "Latitude: " + position.coords.latitude + "
    Longitude: " + position.coords.longitude; } function showError(error) { switch(error.code) { case error.PERMISSION_DENIED: x.innerHTML = "User denied the request for Geolocation." break; case error.POSITION_UNAVAILABLE: x.innerHTML = "Location information is unavailable." break; case error.TIMEOUT: x.innerHTML = "The request to get user location timed out." break; case error.UNKNOWN_ERROR: x.innerHTML = "An unknown error occurred." break; } }

提交回复
热议问题