辅助H5定位
功能场景
本示例展示了在H5页面中如何调用Native定位接口获取位置,并使用百度定位SDK提升定位精度。
//mapopen-website-wiki.bj.bcebos.com/demos/AndroidVideos/辅助H5定位.mp4
enableAssistantLocation(android.webkit.WebView webView)
开发者应用如果有H5页面使用了百度JS定位接口,该接口可以辅助百度JS定位更好的进行定位
disableAssistantLocation()
/**
* 设置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);
}
});
}
复制成功
// 启动H5辅助定位
mLocationClient.enableAssistantLocation(contentWebView);
mLocationClient.start();
复制成功
//mapopen-website-wiki.bj.bcebos.com/demos/iostest.MP4
- (void)clearClusterItems;
- (NSArray<BMKCluster *> *)getClusters:(CGFloat)zoomLevel;
1.第一步
1
/** 更新标注展示. */
- (void)updateClusters {
_clusterZoom = (NSInteger)_mapView.zoomLevel;
@synchronized(_clusterManager.clusterCaches) {
NSMutableArray *clusters = [_clusterManager.clusterCaches objectAtIndex:(_clusterZoom - 3)];
if (clusters.count > 0) {
// 移除一组标注
[_mapView removeAnnotations:_mapView.annotations];
//将一组标注添加到当前地图View中
[_mapView addAnnotations:clusters];
} else {
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 获取聚合后的标注
__block NSArray *array = [weakSelf.clusterManager getClusters:weakSelf.clusterZoom];
dispatch_async(dispatch_get_main_queue(), ^{
for (BMKCluster *item in array) {
ClusterAnnotation *annotation = [[ClusterAnnotation alloc] init];
// 设置标注的经纬度坐标
annotation.coordinate = item.coordinate;
annotation.size = item.size;
// 设置标注的标题
annotation.title = [NSString stringWithFormat:@"我是%lu个", (unsigned long)item.size];
[clusters addObject:annotation];
}
// 移除一组标注
[weakSelf.mapView removeAnnotations:weakSelf.mapView.annotations];
// 将一组标注添加到当前地图View中
[weakSelf.mapView addAnnotations:clusters];
});
});
}
}
}
复制成功
2.第二步
2
/** 更新标注展示. */
- (void)updateClusters {
_clusterZoom = (NSInteger)_mapView.zoomLevel;
@synchronized(_clusterManager.clusterCaches) {
NSMutableArray *clusters = [_clusterManager.clusterCaches objectAtIndex:(_clusterZoom - 3)];
if (clusters.count > 0) {
// 移除一组标注
[_mapView removeAnnotations:_mapView.annotations];
//将一组标注添加到当前地图View中
[_mapView addAnnotations:clusters];
} else {
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 获取聚合后的标注
__block NSArray *array = [weakSelf.clusterManager getClusters:weakSelf.clusterZoom];
dispatch_async(dispatch_get_main_queue(), ^{
for (BMKCluster *item in array) {
ClusterAnnotation *annotation = [[ClusterAnnotation alloc] init];
// 设置标注的经纬度坐标
annotation.coordinate = item.coordinate;
annotation.size = item.size;
// 设置标注的标题
annotation.title = [NSString stringWithFormat:@"我是%lu个", (unsigned long)item.size];
[clusters addObject:annotation];
}
// 移除一组标注
[weakSelf.mapView removeAnnotations:weakSelf.mapView.annotations];
// 将一组标注添加到当前地图View中
[weakSelf.mapView addAnnotations:clusters];
});
});
}
}
}
复制成功
3.名字
3
/** 更新标注展示. */
- (void)updateClusters {
_clusterZoom = (NSInteger)_mapView.zoomLevel;
@synchronized(_clusterManager.clusterCaches) {
NSMutableArray *clusters = [_clusterManager.clusterCaches objectAtIndex:(_clusterZoom - 3)];
if (clusters.count > 0) {
// 移除一组标注
[_mapView removeAnnotations:_mapView.annotations];
//将一组标注添加到当前地图View中
[_mapView addAnnotations:clusters];
} else {
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 获取聚合后的标注
__block NSArray *array = [weakSelf.clusterManager getClusters:weakSelf.clusterZoom];
dispatch_async(dispatch_get_main_queue(), ^{
for (BMKCluster *item in array) {
ClusterAnnotation *annotation = [[ClusterAnnotation alloc] init];
// 设置标注的经纬度坐标
annotation.coordinate = item.coordinate;
annotation.size = item.size;
// 设置标注的标题
annotation.title = [NSString stringWithFormat:@"我是%lu个", (unsigned long)item.size];
[clusters addObject:annotation];
}
// 移除一组标注
[weakSelf.mapView removeAnnotations:weakSelf.mapView.annotations];
// 将一组标注添加到当前地图View中
[weakSelf.mapView addAnnotations:clusters];
});
});
}
}
}
复制成功