Current Location on the react and android webview is working in Android Studio but on the android app

24 views Asked by At

I am facing an issue where Current Location on the react and android webview is working in Android Studio but on the android app. Here is the code that i am using

  const getCurrentLocationNew = () => {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(
        async (position) => {
          const { latitude, longitude } = position.coords;
          const coordinates = { lat: latitude, lng: longitude };
  
          // Reverse geocode the coordinates to get address
          try {
            const results = await geocodeByAddress(`${latitude}, ${longitude}`);
            if (results && results.length > 0) {
              const address = results[0].formatted_address;
              setAddress(address);
              setGlobalAddress(address);
              localStorage.setItem('loc', JSON.stringify(coordinates));
            }
          } catch (error) {
            console.error('Error fetching location:', error);
          }
        },
        (error) => {
          if (error.code === error.PERMISSION_DENIED) {
            console.error('User denied geolocation permission.');
            // Handle denied permission (e.g., show a message to the user)
          } else if (error.code === error.POSITION_UNAVAILABLE) {
            console.error('Geolocation information is unavailable.');
            // Handle unavailable location (e.g., show a message to the user)
          } else if (error.code === error.TIMEOUT) {
            console.error('Geolocation request timed out.');
            // Handle timeout (e.g., show a message to the user)
          } else {
            console.error('An unknown error occurred:', error.message);
            // Handle other errors
          }
        },
        { enableHighAccuracy: false, timeout: 50000, maximumAge: 0 }
      );
    } else {
      console.error('Geolocation is not supported by this browser.');
      // Handle unsupported browser (e.g., show a message to the user)
    }   };
0

There are 0 answers