0

I create a native webview for android based on the react project. I want to access my device location but when I open the app it shows an error: this my permissions added in android:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

In my app.js file I created a useEffect function for accesing my location like this:

useEffect(() => {
    navigator.geolocation.getCurrentPosition(positions => {
      /*
      setLat(positions.coords.latitude);
      setLon(positions.coords.longitude);
      */
      localStorage.setItem("lat", positions.coords.latitude);
      localStorage.setItem("lon", positions.coords.longitude);

    }, err => alert(err));
  }, [])

When I am raning the app it shows alert message and belov is my java code:

if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            AlertDialog .Builder alertDialog;
            alertDialog = new AlertDialog.Builder(MainActivity.this);

            alertDialog.setMessage("permite accesul la locatie")
                    .setTitle("eroare locatie");
            AlertDialog mesajEroare = alertDialog.create();
            mesajEroare.show();


        } else {
            mySite = (WebView)findViewById(R.id.site);
            mySite.getSettings().setGeolocationDatabasePath( MainActivity.this.getFilesDir().getPath() );
            mySite.setWebChromeClient(new WebChromeClient() {
                public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                    callback.invoke(origin, true, false);
                }
            });
            WebSettings setari = mySite.getSettings();
            setari.setJavaScriptEnabled(true);
            setari.setDomStorageEnabled(true);
            setari.setDatabaseEnabled(true);
            setari.setDatabasePath("/data/data/" + mySite.getContext().getPackageName() + "/databases/");
            setari.setGeolocationEnabled(true);
            mySite.loadUrl("http://10.0.2.2:3000/");


        }

I don't know where is the problem! Thank you!

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
Cosmin Ciolacu
  • 406
  • 9
  • 29
  • 1
    Just guessing, but I think the permission is not granted. I'd think you need to show the permission promt yourself in the `onGeolocationPermissionsShowPrompt` function and then call the callback method with the result. You can check the permission state of an app in the system settings. – Jannik Oct 15 '19 at 08:54
  • 1
    You need to request the permissions at runtime. Check this post https://stackoverflow.com/questions/40142331/how-to-request-location-permission-at-runtime – Ignacio Tomas Crespo Oct 15 '19 at 08:58
  • @Jannik Can you shows the code ? – Cosmin Ciolacu Oct 15 '19 at 09:25
  • I cannot show you the code because I've never written it. But check out the question mentioned by Ignacio. The answer there is applicable for you as well. You only need to modify `onRequestPermissionsResult` a bit. Instead of calling `locationManager.requestLocationUpdates` you'd invoke the callback you recieved in `onGeolocationPermissionsShowPrompt`. – Jannik Oct 15 '19 at 15:45

0 Answers0