1

我正在尝试使用自定义在 webView 中打开 Google 地图WebChromeClient并获取我的位置,但地图说它无法设置位置。完整代码:

  import android.Manifest;
  import android.content.pm.PackageManager;
  import android.graphics.Bitmap;
  import android.support.v4.app.ActivityCompat;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.Log;
  import android.webkit.GeolocationPermissions;
  import android.webkit.WebChromeClient;
  import android.webkit.WebResourceRequest;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION
        }, 0);
        return;
    }


    WebView webView = (WebView)findViewById(R.id.wv) ;
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setDatabasePath("");
    webView.getSettings().setGeolocationDatabasePath( this.getFilesDir().getPath() );
    webView.setWebChromeClient(new webChromeClient());
    webView.getSettings().setGeolocationEnabled(true);
    webView.setWebViewClient(new webViewClient());
    webView.getSettings().setGeolocationDatabasePath( 
    webView.getContext().getFilesDir().getPath());
    webView.loadUrl("https://google.ru/maps");




}
class webViewClient extends WebViewClient{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return super.shouldOverrideUrlLoading(view, request);
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }
}

    class webChromeClient extends WebChromeClient{
        @Override
        public void onGeolocationPermissionsHidePrompt() {
            super.onGeolocationPermissionsHidePrompt();
            Log.d("MainActivity","onGeolocationPermissionHidePromt");
        }

        @Override
        public void onGeolocationPermissionsShowPrompt(String origin, 
    GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, true);
            Log.d("MainActivity","onGeolocationPermissionShowPromt");
        }
    }
}

InonGeolocationPermissionsShowPrompt Log.d是沉默的,意味着无效不呼叫。但为什么?

显现:

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

仅在布局中webView

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/wv"/>

问题是什么?提前致谢。ps对不起我的英语。

4

0 回答 0