Open In App
Related Articles

HTML Geolocation clearWatch() Method

Improve Article
Save Article
Like Article

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.




  • clearId: The parameter ‘clearId’ is returned by the watchPosition() call itself. It is used to identify the watchPosition() call which we have to remove.

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


<!DOCTYPE html>
<html lang="en">
    <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>
    <h2>Welcome To GFG</h2>
    <button onclick="getPosition()">
        Watch Position
    <p id="demo"></p>
    <button onclick="clearPosition()">
        Clear Position
        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);


Explaining clearWatch() method

Supported Browsers:

  • Google Chrome 5.0
  • Microsoft Edge 12.0
  • Firefox 3.5
  • Internet Explorer 9.0
  • Safari 5.0
  • Opera 10.6

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 14 Jan, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials