具体步骤:
一、添加权限:
二、设置webview.setting
WebSettings settings = webView.getSettings(); settings.setGeolocationEnabled(true);
三、继承WebChromeClient类并且重写其onGeolocationPermissionsShowPrompt函数
protected class PublicWebChromeClient extends WebChromeClient { public void onGeolocationPermissionsShowPrompt(final String origin, final android.webkit.GeolocationPermissions.Callback callback) { final boolean remember = true; AlertDialog.Builder builder = new AlertDialog.Builder(PublicWebViewActivity.this); builder.setTitle("位置信息"); builder.setMessage(origin + "允许获取您的位置信息吗?").setCancelable(true).setPositiveButton("允许", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { callback.invoke(origin, true, remember); } }) .setNegativeButton("不允许", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { callback.invoke(origin, false, remember); } }); AlertDialog alert = builder.create(); alert.show(); } }
并设置:
webview.setWebChromeClient(new PublicWebChromeClient());
至此Android混合开发webview就能使用内置浏览器的定位了。