HTML5 Game - Geolocation Geolocation Errors

Introduction

We can provide a second argument to the getCurrentPosition method.

It can supply a callback function if there is an error obtaining the location.

The function is passed a PositionError object, which defines the properties described in the following table.

Name Description Returns
code Returns a code indicating the type of error number
message Returns a string that describes the error string

There are three possible values for the PositionError.code property.

Value Description
1 The user did not grant permission to use the geolocation feature
2 The position could not be determined
3 The attempt to request the location timed out

The following code shows how we can receive errors using the PositionError object.

Demo

ResultView the demo in separate window

<!DOCTYPE HTML>  
      <html>  
          <head>  
              <title>Example</title>  
              <style>  
                  table {border-collapse: collapse;}  
                  th, td {padding: 4px;}  
                  th {text-align: right;}  
              </style>  
          </head>  
          <body>  
              <table border="1">  
                  <tr>  
                      <th>Longitude:</th><td id="longitude">-</td>  
                      <th>Latitude:</th><td id="latitude">-</td>  
                  </tr>  
                  <tr>  
                      <th>Altitude:</th><td id="altitude">-</td>  
                      <th>Accuracy:</th><td id="accuracy">-</td>  
                  </tr>  
                  <tr>  
                      <th>Altitude Accuracy:</th><td id="altitudeAccuracy">-</td>  
                      <th>Heading:</th><td id="heading">-</td>  
                  </tr>  
                  <tr>  
                      <th>Speed:</th><td id="speed">-</td>  
                      <th>Time Stamp:</th><td id="timestamp">-</td>  
                  </tr>  
                  <tr>  
                      <th>Error Code:</th><td id="errcode">-</td>  
                      <th>Error Message:</th><td id="errmessage">-</td>  
                  </tr>              
              </table>  
                    //ww  w .  j a v a  2  s .  com
              <script>          
                  navigator.geolocation.getCurrentPosition(displayPosition, handleError);  
                    
                  function displayPosition(pos) {  
                      let properties = ["longitude", "latitude", "altitude", "accuracy",  
                                        "altitudeAccuracy", "heading", "speed"];  
                        
                      for (let i = 0; i < properties.length; i++) {  
                          let value = pos.coords[properties[i]];  
                          document.getElementById(properties[i]).innerHTML = value;  
                      }  
                      document.getElementById("timestamp").innerHTML = pos.timestamp;  
                  }  
                    
                  function handleError(err) {  
                      document.getElementById("errcode").innerHTML = err.code;  
                      document.getElementById("errmessage").innerHTML = err.message;  
                  }  
        </script>  
    </body>  
</html>

To create an error, refuse permission when prompted by the browser.

Related Topics