如何在Android WebView 23+中使Geolocation工作?

时间:2016-12-23 17:54:17

标签: android google-maps-api-3 android-webview google-geolocation

我试图在Android网页浏览中使Geolocation工作无济于事;地理位置在生产站点上的浏览器上运行良好,但navigator.geolocation.getCurrentPosition()始终返回空值。

我认为它与WebView本身有关,因为已经为应用程序授予了使用Geolocation的权限。

如何在WebView中进行Geolocation工作?

这是我的WebView初始化程序:

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WebView myWebView = (WebView) findViewById(R.id.webview);
    client = new GoogleApiClient.Builder(this)
            .addApi(AppIndex.API)
            .addApi(LocationServices.API)
            .build();


    askForPermission(Manifest.permission.ACCESS_FINE_LOCATION,LOCATION);
    getSupportActionBar().hide();
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    //getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    //getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    //getWindow().setStatusBarColor(getResources().getColor(android.R.color.holo_red_dark));
    setContentView(R.layout.activity_main);


    myWebView.setWebViewClient(new GeoWebViewClient());

    myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    myWebView.getSettings().setAppCacheEnabled(true);
    myWebView.getSettings().setDatabaseEnabled(true);
    myWebView.getSettings().setGeolocationEnabled(true);
    myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
    myWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
    myWebView.getSettings().setAllowFileAccess(true);
    myWebView.setWebChromeClient(new GeoWebChromeClient());
    myWebView.loadUrl("https://obscure-dusk-27124.herokuapp.com");

    if (!DetectConnection.checkInternetConnection(this)) {
        myWebView.loadUrl("about:blank");
    }


    myWebView.setWebChromeClient(new WebChromeClient(){
        // Need to accept permissions to use the camera
        @Override
        public void onPermissionRequest(final PermissionRequest request) {

            request.grant(request.getResources());
        }
    });

}

0 个答案:

没有答案