Geolocation API asks for user's approval in web browsers and it's easy to handle that but in mobile browsers prompt to enable location doesn't show up until GPS is enabled. For example, prompt for 'www.abc.com needs your location' shows if and only if GPS is on. Is there any way to prompt for location even GPS is disabled and able it via that prompt?
-
[link](https://forums.xamarin.com/discussion/118189/gps-location-enable-in-xamarin-forms) do you want this in your app? – Kevin Kurien Nov 19 '18 at 10:38
-
No, not in app browser. I need to do this for default android browser and other browsers like chrome, firefox, opera mini etc. – inayatu Nov 20 '18 at 09:18
-
[link](https://cnet2.cbsistatic.com/img/asXTCSYDABpmrXdLKt4-C00--XE=/370x0/2017/02/28/095c9593-2d51-47db-9da6-c648d3735464/mapquest-know-your-location.jpg) you mean this? – Kevin Kurien Nov 20 '18 at 09:38
-
Yes, you got me right. – inayatu Nov 22 '18 at 05:51
1 Answers
JavaScript must be enabled in the
WebView
, using WebSettings.setJavaScriptEnabled(true);The app needs permission
ACCESS_FINE_LOCATION
The
WebView
must use a custom WebChromeClient 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 iswebView.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() );
for more info

- 812
- 6
- 14