clearWatch method

Stops listening for updates to the current geographical location.

Geolocation API Specification, Section 5.1Internet Explorer 9

Syntax

var retval = geolocation.clearWatch(watchId);

Parameters

watchId [in]

Type: Integer

The ID of the watch operation to cancel. This value is returned by the watchPosition method.

Return value

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Standards information

Remarks

If the watchId parameter does not correspond to any watch process started by a previous call to the watchPosition method, this method returns and no further action is taken.

Windows Internet Explorer 9. This property is supported only for webpages displayed in IE9 Standards mode. For more information, see Defining Document Compatibility.

Examples

In the following example, the user can click the Watch Latitude and Longitude button to call the listenForPosition function in the example. The listenForPosition function confirms that the geolocation object is available before calling the watchPosition method in order to start listening for location updates. When an update occurs, the success callback function updates the text boxes in the page by using the new latitude and longitude coordinates. The user can click the Clear Watch button to call the clearWatch method and stop listening for updates. No further callbacks will be invoked after the clearWatch method is called.


<!DOCTYPE html>  
<html>
<head>
<title>Listening for Location Updates</title>
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<script type="text/javascript">
function setText(val, e) {
    document.getElementById(e).value = val;
}
function insertText(val, e) {
    document.getElementById(e).value += val;
}
var nav = null; 
var watchID;
function listenForPosition() {
  if (nav == null) {
      nav = window.navigator;
  }
  if (nav != null) {
      var geoloc = nav.geolocation;
      if (geoloc != null) {
          watchID = geoloc.watchPosition(successCallback, errorCallback);
      }
      else {
          console.log("Geolocation not supported");
      }
  }
  else {
      console.log("Navigator not found");
  }
}
function clearWatch(watchID) {
    window.navigator.geolocation.clearWatch(watchID);
}
function successCallback(position)
{
   setText(position.coords.latitude, "latitude");
   setText(position.coords.longitude, "longitude");
}
 
function errorCallback(error) {
    var message = "";   
    // Check for known errors
    switch (error.code) {
        case error.PERMISSION_DENIED:
            message = "This website does not have permission to use " + 
                      "the Geolocation API";
            break;
        case error.POSITION_UNAVAILABLE:
            message = "The current position could not be determined.";
            break;
        case error.PERMISSION_DENIED_TIMEOUT:
            message = "The current position could not be determined " + 
                      "within the specified timeout period.";            
            break;
    }
    // If it's an unknown error, build a message that includes 
    // information that helps to identify the situation so that 
    // the error handler can be updated.
    if (message == "")
    {
        var strErrorCode = error.code.toString();
        message = "The position could not be determined due to " + 
                  "an unknown error (Code: " + strErrorCode + ").";
    }
    console.log(message);
}
</script>
</head>
<body>
<label for="latitude">Latitude: </label><input id="latitude" /> <br />
<label for="longitude">Longitude: </label><input id="longitude" /> <br />
<input type="button" onclick="listenForPosition ()" value="Watch Latitude and Longitude"  /> 
<input type="button" value="Clear watch" onclick="clearWatch()" />
</body>
</html>

See also

Reference
IWebGeolocation
geolocation

 

 

Show:
© 2014 Microsoft