Collapse the table of content
Expand the table of content

clearWatch method

Stops listening for updates to the current geographical location.

Geolocation API Specification, Section 5.1Internet Explorer 9



var retval = geolocation.clearWatch(watchId);


watchId [in]

Type: Integer

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

Return value


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

Standards information


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.


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>  
<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) {
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";
        case error.POSITION_UNAVAILABLE:
            message = "The current position could not be determined.";
            message = "The current position could not be determined " + 
                      "within the specified timeout period.";            
    // 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 + ").";
<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()" />

See also




© 2015 Microsoft