Android Webview Geolocation

Android WebView geolocation

OK, After a some time of searching what could be the problem, I hope this will help others too.
The problem in this case it is not my code, but the Android version i was running on the emulator. (I was using Android 7 Nougat).

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request.

For full assistance Please Click Here

Android Webview: navigator.geolocation API is not working, throwing exception POSITION_UNAVAILABLE

Let's check the permission code the issue is in calling of it. and I don't know why you're calling permission in this way as, we've lot of mediums by which you can easily call it. Let's use nabinbhandari/Android-Permissions it's easy to use.

Call in this way:

@Override
protected void onCreate(Bundle savedInstanceState) {

.....................


String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION};
Permissions.check(this/*context*/, permissions, null/*rationale*/, null/*options*/, new
PermissionHandler() {
@Override
public void onGranted() {
// call your task

// webview initilization
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new GeoWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setGeolocationEnabled(true);

/// Below required for geolocation
webView.setWebChromeClient(new GeoWebChromeClient());
webView.getSettings().setGeolocationDatabasePath(getFilesDir().getPath());
webView.loadUrl("<my_webapp_url>");


}

@Override
public void onDenied(Context context, ArrayList<String> deniedPermissions) {
// permission denied, block the feature.
Toast.makeText(this,"Permission denied",Toast.LENGTH_SHORT);
}

});

Or implement these permission overrides function on basis of your need.

Android webview not sending geolocation to javascript

Try this -- permission has been added

package com.nony.myapplication;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

WebView myWebview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//added this line for permission request
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
myWebview = (WebView) findViewById(R.id.webview);
WebSettings websettings = myWebview.getSettings();

myWebview.setWebViewClient(new WebViewClient());
websettings.setJavaScriptEnabled(true);
websettings.setJavaScriptCanOpenWindowsAutomatically(true);
websettings.setGeolocationDatabasePath(getFilesDir().getPath());

myWebview.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});

websettings.setAppCacheEnabled(true);
websettings.setDatabaseEnabled(true);
websettings.setDomStorageEnabled(true);

myWebview.loadUrl("https://example.com/proj/");
}


//newly added for permission
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}


public boolean checkLocationPermission()
{
String permission = "android.permission.ACCESS_FINE_LOCATION";
int res = this.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}

}

WebChromeClient doesn't ask for location permission anymore

Yes. I had a similar problem. Location permissions for an android webview and geolocation are no longer handled by the webChromeClient. They are now handled in the same way as an Android native app. I used the following as a guide and it worked well.

https://developer.android.com/training/location/permissions

getting location in WebView via js

I am not sure why, but now it works. There is code for simply app with webView which use gps location.

MainActivity:

package com.qiteq.gpswebview;

import android.Manifest;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

WebView wv = new WebView(this);
wv.loadUrl("http://qiteq.pl/stack/index.html");
setContentView(wv);

ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
}, 0);

wv.getSettings().setJavaScriptEnabled(true);


wv.setWebChromeClient(new WebChromeClient() {
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});


}
}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qiteq.gpswebview">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

And index.html:

<script>
function getLocationConstant()
{
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(onGeoSuccess,onGeoError);
} else {
alert("No GPS support");
}
}


function onGeoSuccess(event)
{
document.getElementById("location").value = event.coords.latitude+", "+event.coords.longitude;
alert("Success: "+event.coords.latitude+", "+event.coords.longitude);
}


function onGeoError(event)
{
alert("Error code " + event.code + ". " + event.message);
}


</script>

<input type="text" name="location" id="location" style="width:278px;">
<button onclick="getLocationConstant()" >Click</button>


Related Topics



Leave a reply



Submit