Android: Using html5 to determine geolocation in webview with javascript api

android, cordova, geolocation, html, webview

Solution

Add `onGeolocationPermissionsShowPrompt()` to `MyChromeWebViewClient` as below:

private class MyChromeWebViewClient extends WebChromeClient {

    @Override
    public void onProgressChanged(WebView view, int progress) {
        // Activities and WebViews measure progress with different scales.
        // The progress meter will automatically disappear when we reach 100%
        activity.setProgress(progress * 100);
    }

    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        Log.d(LOG_TAG, message);
        // This shows the dialog box.  This can be commented out for dev
        AlertDialog.Builder alertBldr = new AlertDialog.Builder(activity);
        alertBldr.setMessage(message);
        alertBldr.setTitle("Alert");
        alertBldr.show();
        result.confirm();
        return true;
    }

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

You need to import "`android.webkit.GeolocationPermissions`".

Add this permission:

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

This will work I guess.

Problem

I'm currently having an issue with geolocation in a webview. I have a webapp. I'm currently not using phonegap or any other mobile framework. I've been unsuccessful at getting the built-in html5 geolocation javascript api to work on an application that runs in a webview in an android app. The site works fine otherwise from the chrome browser on android 2.0+ (geolocation supported). I'm compiling against android api version 5. I've read this post already Phonegap's solution of writing a proxy which wraps the built in call and uses the host activity instead is good, but I'd prefer to use the built in to the webview (webkit) without using phone gap. I've set the proper permissions in the manifest file: ``` <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_GPS" /> <uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" /> <uses-permission android:name="android.permission.ACCESS_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ``` Here is an example code snippet: ``` webview = (WebView) findViewById(R.id.webview); pbarDialog = new ProgressDialog(this); pbarDialog.setCancelable(false); pbarDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); webview.setWebViewClient(new MyWebViewClient()); webview.getSettings().setJavaScriptEnabled(true); webview.setWebChromeClient(new MyChromeWebViewClient()); webview.setVerticalScrollBarEnabled(false); WebSettings webSettings = webview.getSettings(); webSettings.setSavePassword(true); webSettings.setSaveFormData(true); webSettings.setJavaScriptEnabled(true); webSettings.setSupportZoom(false); webSettings.setJavaScriptCanOpenWindowsAutomatically(true); webSettings.setGeolocationEnabled(true); ``` ... ``` private class MyChromeWebViewClient extends WebChromeClient { @Override public void onProgressChanged(WebView view, int progress) { // Activities and WebViews measure progress with different scales. // The progress meter will automatically disappear when we reach 100% activity.setProgress(progress * 100); } @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { Log.d(LOG_TAG, message); // This shows the dialog box. This can be commented out for dev AlertDialog.Builder alertBldr = new AlertDialog.Builder(activity); alertBldr.setMessage(message); alertBldr.setTitle("Alert"); alertBldr.show(); result.confirm(); return true; } } private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { } } ``` Has anyone else had issues with getting a web application to work in the webview?

Original source

Related problems