Android webview位置检测javascript

时间:2012-02-24 07:00:24

标签: javascript android webview location

我有一个网页,可以在网络浏览器中成功检测到位置。根据位置,我的结果被过滤掉了。我在Android应用程序的webview中运行相同的网站URL。但在Android应用程序中,位置检测javascript无法正常工作。有什么建议吗?

提前致谢。

3 个答案:

答案 0 :(得分:2)

执行上述操作仍然无效,但添加了一些额外的代码 - 请参阅Android Webview Geolocation

我将以下内容添加到我的webview对象

    // 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);
        }
    });

出于某种原因,android.webkit.GeolocationPermissions.Callback必须是完全限定的className,在将其添加为导入之后,Eclipse IDE仍然无法识别它。

答案 1 :(得分:1)

您应该为webview启用javascript。

要做到这一点,

wv = (WebView) findViewById(R.id.web_view);
wv.getSettings().setJavascriptEnabled(true);

答案 2 :(得分:1)

只有一个问题:位置检测JS是否可以在Android浏览器中运行(而不是在Android WebView中)?

要尝试的一件事是将以下内容添加到清单文件中:

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

第二个是在webview中设置JavaScriptEnabled(true)。我刚刚注意到有人回答了这个问题。

在此处阅读更多内容:http://developer.android.com/guide/topics/location/obtaining-user-location.html