多模式定位
功能场景
本示例展示了如何通过配置不同定位模式、坐标系获取精准定位结果及丰富定位信息。
//mapopen-website-wiki.bj.bcebos.com/demos/AndroidVideos/多方式定位.mp4
setLocationMode(LocationClientOption.LocationMode mode)
setIsNeedLocationDescribe(boolean isNeedLocationDescribe)
设置是否需要返回位置语义化信息,可以在BDLocation.getLocationDescribe()中得到数据
setIsNeedAddress(boolean isNeed)
设置是否需要地址信息,默认为无地址。 在设备获取gps位置的时候,对应的地址信息不会马上返回,因为gps是直接在本地产生,但地址信息需要从云端获取,是个异步过程,开发者在接收定位返回数据的时候,建议不要立即stop定位sdk以等待数据回调
setIsNeedLocationPoiList(boolean isNeedLocationPoiList)
设置是否需要返回位置POI信息,可以在BDLocation.getPoiList()中得到数据
setCoorType(java.lang.String coorType)
setLocOption(LocationClientOption locOption)
BDAbstractLocationListener
BDAbstractLocationListener
/**
* 初始化定位
*/
private void initLoctionClient() {
// 定位服务的客户端。宿主程序在客户端声明此类,并调用,目前只支持在主线程中启动
mLocationClient = new LocationClient(getApplicationContext());
mMyLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mMyLocationListener);
mLocationClientOption = new LocationClientOption();
// 可选,默认false,设置是否开启Gps定位
mLocationClientOption.setOpenGps(true);
// 可选,默认false,设置定位时是否需要海拔信息,默认不需要,除基础定位版本都可用
mLocationClientOption.setIsNeedAltitude(true);
// 可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
mLocationClientOption.setLocationMode(LocationMode.Hight_Accuracy);
// 可选,默认gcj02,设置返回的定位结果坐标系,如果配合百度地图使用,建议设置为bd09ll;
mLocationClientOption.setCoorType("bd09ll");
// 设置发起连续定位请求的间隔
mLocationClientOption.setScanSpan(3000);
/ 可选,设置是否需要地址信息,默认不需要
mLocationClientOption.setIsNeedAddress(true);
// 可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
mLocationClientOption.setIsNeedLocationPoiList(true);
// 可选,设置是否需要设备方向结果
mLocationClientOption.setNeedDeviceDirect(true);
// 可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
mLocationClientOption.setIsNeedLocationDescribe(true);
// 需将配置好的LocationClientOption对象,通过setLocOption方法传递给LocationClient对象使用
mLocationClient.setLocOption(mLocationClientOption);
// 开始定位
mLocationClient.start();
}
复制成功
/**
* 定位信息回调
*/
class MyLocationListener extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
// 在此获取定位结果信息。
}
}
复制成功
//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];
});
});
}
}
}
复制成功