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!