- JavaScript must be enabled in the
WebView
, usingWebSettings.setJavaScriptEnabled(true);
- The app needs permission
ACCESS_FINE_LOCATION
-
The
WebView
must use a customWebChromeClient
which implementsWebChromeClient.onGeolocationPermissionsShowPrompt()
. This method
is called by theWebView
to obtain permission to disclose the user’s location to JavaScript. (In the case of the browser, we show a prompt to the user.) The default implementation does nothing, so permission is never obtained and the location is never passed to JavaScript. A simple implementation which always grants permission is …webView.setWebChromeClient(new WebChromeClient() { public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { callback.invoke(origin, true, false); } });
Geolocation uses databases to persist cached positions and permissions between sessions. The location of the database is set using WebSettings.setGeolocationDatabasePath(...)
. If the location of the database is not set, the persistent storage will not be available, but Geolocation will continue to function correctly otherwise. To set the location of the databases, use …
webView.getSettings().setGeolocationDatabasePath( context.getFilesDir().getPath() );