How to make Geolocation work in Android WebView 23+?

215 views Asked by At

I am trying to make Geolocation work in an Android webview to no avail; Geolocation works fine on browsers on the production site, but navigator.geolocation.getCurrentPosition() always returns an empty value.

I believe it has something to do with the WebView itself, since the permissions for using Geolocation are already granted for the app.

How can I make Geolocation work inside the WebView?

Here is my WebView initializer:

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WebView myWebView = (WebView) findViewById(R.id.webview);
    client = new GoogleApiClient.Builder(this)
            .addApi(AppIndex.API)
            .addApi(LocationServices.API)
            .build();


    askForPermission(Manifest.permission.ACCESS_FINE_LOCATION,LOCATION);
    getSupportActionBar().hide();
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    //getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    //getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    //getWindow().setStatusBarColor(getResources().getColor(android.R.color.holo_red_dark));
    setContentView(R.layout.activity_main);


    myWebView.setWebViewClient(new GeoWebViewClient());

    myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    myWebView.getSettings().setAppCacheEnabled(true);
    myWebView.getSettings().setDatabaseEnabled(true);
    myWebView.getSettings().setGeolocationEnabled(true);
    myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
    myWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
    myWebView.getSettings().setAllowFileAccess(true);
    myWebView.setWebChromeClient(new GeoWebChromeClient());
    myWebView.loadUrl("https://obscure-dusk-27124.herokuapp.com");

    if (!DetectConnection.checkInternetConnection(this)) {
        myWebView.loadUrl("about:blank");
    }


    myWebView.setWebChromeClient(new WebChromeClient(){
        // Need to accept permissions to use the camera
        @Override
        public void onPermissionRequest(final PermissionRequest request) {

            request.grant(request.getResources());
        }
    });

}
0

There are 0 answers