浏览器版本低!无法浏览完整内容,建议升级或更换浏览器。

文档全面上新

更科技的视觉体验,更高效的页面结构,快前往体验吧!

体验新版
辅助H5定位
功能场景
本示例展示了在H5页面中如何调用Native定位接口获取位置,并使用百度定位SDK提升定位精度。
Android
iOS
//mapopen-website-wiki.bj.bcebos.com/demos/AndroidVideos/辅助H5定位.mp4
1590746640|//mapopen-website-wiki.bj.bcebos.com/demos/newqrcodes/辅助H5定位.png
扫码体验
使用产品
Android定位SDK|/index.php?title=android-locsdk
>
辅助H5定位|/index.php?title=android-locsdk/guide/addition-func/assistant-h5
下载源码
//mapopen-website-wiki.bj.bcebos.com/demos/location/AssistLocationDemo.zip
核心接口
接口
描述
LocationClient
enableAssistantLocation(android.webkit.WebView webView)
开发者应用如果有H5页面使用了百度JS定位接口,该接口可以辅助百度JS定位更好的进行定位
LocationClient
disableAssistantLocation()
停止H5辅助定位
LocationClient
start()
开启定位
LocationClient
开启定位
关闭定位
核心代码
1.设置WebView属性加载H5页面
JAVA
/**
* 设置setWebView
*/
private void setWebView(){
    contentWebView = (WebView) findViewById(R.id.webview);
    contentWebView.loadUrl("file:///android_asset/AssistantLoc.html");
    WebSettings webSettings = contentWebView.getSettings();
    // 允许webview执行javaScript脚本
    webSettings.setJavaScriptEnabled(true);
    // 设置是否允许定位,这里为了使用H5辅助定位,设置为false。
    // 设置为true不一定会进行H5辅助定位,设置为true时只有H5定位失败后才会进行辅助定位
//    webSettings.setGeolocationEnabled(false);
    contentWebView.setWebViewClient(new WebViewClient() {

        // 页面请求完成
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        }

        // 页面开始加载
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }
    });

    contentWebView.setWebChromeClient(new WebChromeClient() {
        // 处理javascript中的alert
        public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
            return true;
        };

        // 处理javascript中的confirm
        public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
            return true;
        };

        // 处理定位权限请求
        @Override
        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, false);
            super.onGeolocationPermissionsShowPrompt(origin, callback);
        }
        @Override
        // 设置网页加载的进度条
        public void onProgressChanged(WebView view, int newProgress) {
            MainActivity.this.getWindow().setFeatureInt(
                    Window.FEATURE_PROGRESS, newProgress * 100);
            super.onProgressChanged(view, newProgress);
        }

        // 设置应用程序的标题title
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
        }
    });
}
                
复制
深色
复制成功
2.开启辅助定位
JAVA
// 启动H5辅助定位
mLocationClient.enableAssistantLocation(contentWebView);
mLocationClient.start();
                
复制
深色
复制成功
  • 文档根本没法用

  • 文档水平很差

  • 文档水平一般

  • 文档不错

  • 文档写的很好

如发现文档错误,或对此文档有更好的建议,请在下方反馈。问题咨询请前往反馈平台提交工单咨询。

提交反馈

拖动标注工具

添加矩形标注

添加箭头标注

完成

取消