0

I need to get the location of a user from within a webview and it's not asking me for permission to get the location. Below is my webview code:

WebView Setup

public void WebViewSettings(){

    webView = (WebView) findViewById(R.id.webview);
    webView.canGoBack();

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.setWebViewClient(new WebViewClientSetup());
    //webView.setWebChromeClient(new GeoWebChromeClient());
    webView.getSettings().setGeolocationEnabled(true);
    //webView.getSettings().setGeolocationDatabasePath( context.getFilesDir().getPath() );

    myTimer = new Timer();
    //Start this timer when you create you task
    myTimer.schedule(new loaderTask(), 20000);

}

WebChromeClient:

public class GeoWebChromeClient extends WebChromeClient {
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
        callback.invoke(origin, true, false);
    }
}

WebViewClient:

public class WebViewClientSetup extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;
    }

    @Override
    public void onLoadResource(WebView view, String url) {
        // Check to see if there is a progress dialog
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(context);
            progressDialog.setTitle("Loading...");
            progressDialog.setMessage("Please wait.");
            progressDialog.setCancelable(true);
            progressDialog.setIndeterminate(true);
            progressDialog.show();
        }
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler,
            SslError error) {
        handler.proceed();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        isPageLoadedComplete = true;
        View mapHeight = (View) findViewById(R.id.webview);
        int height = mapHeight.getHeight();
        Log.d("map height", "map height" + height);
        // Page is done loading;
        // hide the progress dialog and show the webview
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    @Override
    public void onReceivedError(WebView view, int errorCod,
            String description, String failingUrl) {
        AlertDialog();
    }
}

it's not asking me for permission and I don't know why, can anyone see where I'm going wrong? Thanks in advance!

Paddy1990
  • 946
  • 4
  • 13
  • 30

1 Answers1

1

You should add the permissions that you want to use in your android-manifest.xml. Add the following lines to the android-manifest.xml.

<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" />
sazz
  • 3,193
  • 3
  • 23
  • 33
  • I already had `` && `` and I've added the others but it doesn't seem to do the trick, I assume I'm supposed to get a popup asking me for permission to use my location? – Paddy1990 May 06 '14 at 14:19
  • it may be a silly question, but are you testing on real device? because the code looks correct, and you should got the popup asking you for permission to use your location. – sazz May 06 '14 at 15:52
  • yeah on about 5 different real devices, I think I needed to add more permissions to the manefest file including `` && ``. I also found out that if I want an alert to pop up asking the user for permission to use their location, I have to manually add it into the `onGeolocationPermissionsShowPrompt` method with an `AlertBuilder` – Paddy1990 May 06 '14 at 15:57
  • did you finally get this up and running? – C graphics Feb 02 '15 at 17:21
  • This question was previously answered here: http://stackoverflow.com/questions/5329662/android-webview-geolocation – smoothBlue Feb 13 '15 at 04:17