Search code examples
javascriptreactjsgeolocationnavigator

GeoLocation giving "Permission was revoked" in Chrome and not other browsers even though I can't retrieve location data in any


I am working with the Geolocation API in a React project and on calling the API, I am receiving a callback error message that permission is revoked. I have toggled on and off the location popup and also reset the location to be available for all applications in my OS settings. I tested this on Chrome, Brave and Edge with the location access allowed. On all browsers, I have no location data available but in Chrome, I get a "Permission was revoked" error also.

I am trying to save the values of the latitude and longitude into a React state.

   const [location, setLocation] = useState({
     latitude: "",
     longitude: "",
   });
  

  function successCallback(position) {
    const latitude = position.coords.latitude;
    const longitude = position.coords.longitude;
    setLocation(prevValue => ({
      ...prevValue,
      latitude,
      longitude,
    }));
  }

  function errorCallback(error) {
    console.log("Error", error);
  }

  navigator.geolocation.getCurrentPosition(successCallback, errorCallback);

When I console log the location state inside the successCallback function, I have no data, calling it inside of my main render returns just an object with empty values set to the latitude and longitude in the location state.

My problem is that, how do I get the location data to be retrieved?


Solution

  • I had this issue earlier today but restarting Chrome should fix it. To be sure, try reseting data + permissions for the page you're having that issue before restarting Chrome.

    You can do that though chrome://settings/content/all and searching for the URL or though the Lock icon next to the url