简体   繁体   English

Android Webview位置检测javascript

[英]Android webview location detect javascript

I have a web page in which location is getting detected in web browser successfully. 我有一个网页,该网页已成功在Web浏览器中检测到位置。 And based on location my results are filtered out. 根据位置,我的结果将被过滤掉。 I am running same website url in a webview of android app. 我在android应用的webview中运行相同的网站url。 But in android app that location detection javascript is not working. 但是在android应用中,位置检测javascript无法正常工作。 Any suggestions what to do here? 有什么建议在这里做什么?

Thanks in advance. 提前致谢。

Doing the above still didn't work for me, but adding some additional code did - see Android Webview Geolocation 进行上述操作对我来说仍然行不通,但添加一些其他代码行了-请参阅Android Webview地理位置

I added the following to my webview object 我将以下内容添加到我的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);
        }
    });

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. 由于某种原因, android.webkit.GeolocationPermissions.Callback必须是完全合格的className,将其添加为导入后,Eclipse IDE仍无法识别它。

You should enable javascript for webview. 您应该为网页视图启用javascript。

To do that, 要做到这一点,

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

Just one question: Does the location detection JS work in the Android Browser (not in the Android WebView)? 只是一个问题:位置检测JS是否可以在Android浏览器(而不是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. 第二个是在您的Web视图中设置setJavaScriptEnabled(true)。 I just noticed someone answered this below. 我只是注意到有人在下面回答了这个问题。

Read more here: http://developer.android.com/guide/topics/location/obtaining-user-location.html 在此处阅读更多信息: http : //developer.android.com/guide/topics/location/obtaining-user-location.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM