Open In App

HTML Geolocation clearWatch() Method

In this article, we will learn the HTML geolocation clearWatch() method and its implementation. The clearWatch() method is used to cancel the currently running watchPosition() call and removes the location or errors generated as a result of the previous call.

Syntax:



navigator.geolocation.clearWatch(clearId);

Parameter:

Example: The example below illustrate the geolocation clearWatch() method:






<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <title>HTML Geolocation clearWatch() Method</title>
</head>
  
<body>
    <h2>Welcome To GFG</h2>
  
    <button onclick="getPosition()">
        Watch Position
    </button>
      
    <p id="demo"></p>
  
    <button onclick="clearPosition()">
        Clear Position
    </button>
  
    <script>
        var clearId;
        var demoPara = document.getElementById("demo");
        var user_loc = navigator.geolocation;
  
        function getPosition() {
            if (user_loc) {
                var option = { timeout: 50000 }
                clearId = user_loc.watchPosition(
                    currentLoc, resolveError, option);
            }
            else {
                "Your browser doesn't support Geolocation API.";
            }
        }
  
        function currentLoc(loc) {
            var lat = loc.coords.latitude;
            var long = loc.coords.longitude;
            demoPara.innerHTML = "Latitude: " 
                + lat + "<br>Longitude: " + long;
        }
  
        function resolveError(err) {
            demoPara.innerHTML = "ERROR" 
                + err.code + ": " + err.message;
        }
  
        function clearPosition() {
            demoPara.innerHTML = user_loc.clearWatch(clearId);
        }
    </script>
</body>
  
</html>

Output:

Explaining clearWatch() method

Supported Browsers:


Article Tags :