問題描述
我目前在 web 視圖中遇到地理定位問題.我有一個網絡應用程序.我目前沒有使用 phonegap 或任何其他移動框架.我無法讓內置的 html5 地理定位 javascript api 用于在 android 應用程序的 webview 中運行的應用程序.該網站在 android 2.0+(支持地理位置)上的 chrome 瀏覽器上運行良好.
I'm currently having an issue with geolocation in a webview. I have a webapp. I'm currently not using phonegap or any other mobile framework. I've been unsuccessful at getting the built-in html5 geolocation javascript api to work on an application that runs in a webview in an android app. The site works fine otherwise from the chrome browser on android 2.0+ (geolocation supported).
我正在針對 android api 版本 5 進行編譯.
I'm compiling against android api version 5.
我讀過這篇文章已經
Phonegap 的解決方案是編寫一個封裝內置調用并使用主機活動的代理,但我更喜歡在不使用電話間隙的情況下使用內置到 webview (webkit).
Phonegap's solution of writing a proxy which wraps the built in call and uses the host activity instead is good, but I'd prefer to use the built in to the webview (webkit) without using phone gap.
我已經在清單文件中設置了適當的權限:
I've set the proper permissions in the manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
這是一個示例代碼片段:
Here is an example code snippet:
webview = (WebView) findViewById(R.id.webview);
pbarDialog = new ProgressDialog(this);
pbarDialog.setCancelable(false);
pbarDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
webview.setWebViewClient(new MyWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new MyChromeWebViewClient());
webview.setVerticalScrollBarEnabled(false);
WebSettings webSettings = webview.getSettings();
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setGeolocationEnabled(true);
...
private class MyChromeWebViewClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 100);
}
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.d(LOG_TAG, message);
// This shows the dialog box. This can be commented out for dev
AlertDialog.Builder alertBldr = new AlertDialog.Builder(activity);
alertBldr.setMessage(message);
alertBldr.setTitle("Alert");
alertBldr.show();
result.confirm();
return true;
}
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
}
}
有沒有其他人在讓 web 應用程序在 webview 中工作時遇到問題?
Has anyone else had issues with getting a web application to work in the webview?
推薦答案
將 onGeolocationPermissionsShowPrompt()
添加到 MyChromeWebViewClient
如下:
Add onGeolocationPermissionsShowPrompt()
to MyChromeWebViewClient
as below:
private class MyChromeWebViewClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int progress) {
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
activity.setProgress(progress * 100);
}
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.d(LOG_TAG, message);
// This shows the dialog box. This can be commented out for dev
AlertDialog.Builder alertBldr = new AlertDialog.Builder(activity);
alertBldr.setMessage(message);
alertBldr.setTitle("Alert");
alertBldr.show();
result.confirm();
return true;
}
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
}
您需要導入android.webkit.GeolocationPermissions
".
添加此權限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
我猜這會奏效.
這篇關于Android:使用 html5 使用 javascript api 確定 webview 中的地理位置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!