天天看點

android webview 擷取Geolocation

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

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

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

private WebViewwebView = null;

private WebSettingswebSettings = null;

private void initWeb() {

webView = (WebView)this.findViewById(R.id.webView_load);

webView.getSettings().setJavaScriptEnabled(true);

loadURL(urlStr);

setClient();

webSettings =webView.getSettings();// 網頁設定預設屬性

webView.setInitialScale(39);// 适應豎屏

webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);// 去掉底部和右邊的滾動條

// webSettings.setRenderPriority(RenderPriority.HIGH);// 提高渲染優先級

webSettings.setJavaScriptEnabled(true);// 設定是否可以互動Javascript

webSettings.setAllowFileAccess(true);// 啟用或禁止WebView通路檔案資料

webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);// 設定顯示模式

webSettings.setDefaultZoom(ZoomDensity.MEDIUM);// 适應螢幕

// webView.getSettings().setUseWideViewPort(true);// 是否任意比例縮放

webView.getSettings().setLoadWithOverviewMode(true);// 縮放至螢幕的大小

// webView.getSettings().setUseWideViewPort(true);// 是否任意比例縮放

webSettings.setBuiltInZoomControls(false);// 設定是否支援縮放

// webSettings.setSupportZoom(false);// 設定是否支援變焦,僅支援輕按兩下縮放

webSettings.setNeedInitialFocus(false);// 設定是否可以通路檔案

// webSettings.setBlockNetworkImage(true);// 加載圖檔放在最後加載渲染

// webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//

// 設定緩存模式

webSettings.setCacheMode(webSettings.LOAD_NO_CACHE);// 不使用緩存

webView.addJavascriptInterface(new Scan2(),"mMap"); // 這裡的名稱就是在頁面window.fxl.testFunction()所取的别名

webView.addJavascriptInterface(new Scan2(),"phone"); // 這裡的名稱就是在頁面window.fxl.testFunction()所取的别名

//關鍵setGeolocationEnabled

webView.getSettings().setGeolocationEnabled(true);

}

private void setClient() {

webView.setWebChromeClient(new WebChromeClient() {

@Override

public void onGeolocationPermissionsShowPrompt(String origin, 

android.webkit.GeolocationPermissions.Callback callback) {   

callback.invoke(origin, true, false);     

super.onGeolocationPermissionsShowPrompt(origin, callback);  

        } 

}