0

I have a web page in which location is getting detected in web browser successfully. And based on location my results are filtered out. I am running same website url in a webview of android app. But in android app that location detection javascript is not working. Any suggestions what to do here?

Thanks in advance.

Ruchir Shah
  • 898
  • 3
  • 13
  • 31

3 Answers3

3

Doing the above still didn't work for me, but adding some additional code did - see Android Webview Geolocation

I added the following to my webview object

    // enable geolocationEnabled
    webview.getSettings().setGeolocationEnabled(true);

    // enable JavaScriptCanOpenWindowsAutomatically, not sure if this is needed
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

    // set setWebChromeClient
    webview.setWebChromeClient(new WebChromeClient() {
        public void onGeolocationPermissionsShowPrompt(String origin, android.webkit.GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, false);
        }
    });

For some reason android.webkit.GeolocationPermissions.Callback had to be a fully qualify className, after adding it as an import it was still not recognized by the Eclipse IDE.

Community
  • 1
  • 1
Rob Lowe
  • 31
  • 2
1

You should enable javascript for webview.

To do that,

wv = (WebView) findViewById(R.id.web_view);
wv.getSettings().setJavascriptEnabled(true);
Sudarshan Bhat
  • 3,772
  • 2
  • 26
  • 53
1

Just one question: Does the location detection JS work in the Android Browser (not in the Android WebView)?

One thing to try is add the following to your manifest file:

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

The second is to setJavaScriptEnabled(true) in your webview. I just noticed someone answered this below.

Read more here: http://developer.android.com/guide/topics/location/obtaining-user-location.html

Arjun
  • 1,701
  • 4
  • 17
  • 25