問題描述
我必須在 WebView
中檢索用戶的位置.我使用以下 Javascript 執行此操作:
I have to retrieve a user's location in a WebView
. I do this with the following Javascript:
function getLocation() {
navigator.geolocation.getCurrentPosition(displayLocation, handleError);
}
但是權限請求彈出窗口永遠不會打開.
But the permission request popup never opens.
我已經設置了這些設置:
I've set these settings:
ws.setJavaScriptEnabled(true);
ws.setGeolocationEnabled(true);
ws.setJavaScriptCanOpenWindowsAutomatically(true);
從 WebView
中訪問用戶位置的正確方法是什么?
What is the correct way to access a user's location from within a WebView
?
推薦答案
- 必須在
WebView
中啟用 JavaScript,使用WebSettings.setJavaScriptEnabled(true);
- 應用需要權限
ACCESS_FINE_LOCATION
WebView
必須使用實現WebChromeClient.onGeolocationPermissionsShowPrompt()
的自定義WebChromeClient
.這種方法由WebView
調用以獲取向 JavaScript 公開用戶位置的權限.(在瀏覽器的情況下,我們向用戶顯示一個提示.)默認實現什么都不做,所以永遠不會獲得許可,也永遠不會將位置傳遞給 JavaScript.始終授予權限的簡單實現是...- JavaScript must be enabled in the
WebView
, usingWebSettings.setJavaScriptEnabled(true);
- The app needs permission
ACCESS_FINE_LOCATION
The
WebView
must use a customWebChromeClient
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 is ...webView.setWebChromeClient(new WebChromeClient() { public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { callback.invoke(origin, true, false); } });
地理定位使用數據庫在會話之間保持緩存的位置和權限.使用
WebSettings.setGeolocationDatabasePath(...)
設置數據庫的位置.如果未設置數據庫的位置,則持久存儲將不可用,但 Geolocation 將繼續正常運行,否則.要設置數據庫的位置,請使用 ...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() );
這篇關于android webview 地理定位的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!- JavaScript must be enabled in the