百度点聚合功能,针头自定义功能

关于怎么导入百度地图SDK与创建应用就不多说了,百度的文档应该比我说的更详细,下面直接正文吧:

1.首先地图的初始化

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)setupView {

    /// 地图
    _mapView = [[BMKMapView alloc]init];
    
    [_mapView setMapType:BMKMapTypeStandard];// 地图类型 ->卫星/标准、
    _mapView.showsUserLocation = YES;
    _mapView.gesturesEnabled = YES;
    _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
    _mapView.frame = self.view.bounds;
    [self.view addSubview:_mapView];
}
// 地图的初始化
- (void)setupMapService {
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    _locService.desiredAccuracy =  kCLLocationAccuracyBest;
    _locService.distanceFilter = 10;//大于100米
    [_locService startUserLocationService];
    
    _geoCodeSerch = [[BMKGeoCodeSearch alloc] init];
    _geoCodeSerch.delegate = self;
    
    _mapView.showsUserLocation = NO;//先关闭显示的定位图层
    _mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态
    _mapView.showsUserLocation = YES;//显示定位图层
    
    _clusterManager = [[BMKClusterManager alloc] init];
    
    //初始化检索对象
    self.districtSearch = [[BMKDistrictSearch alloc] init];
    //设置delegate,用于接收检索结果
    self.districtSearch.delegate = self;
    
    //在此处理正常结果
    _clusterCaches = [[NSMutableArray alloc] init];
    for (NSInteger i = 3; i < 22; i++) {
        [_clusterCaches addObject:[NSMutableArray array]];
    }
}

2.地图的开始与加载完毕

/**
 *地图初始化完毕时会调用此接口
 *@param mapView 地图View
 */
- (void)mapViewDidFinishLoading:(BMKMapView *)mapView {
    BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc]init];
    displayParam.isAccuracyCircleShow = NO;//精度圈是否显示
    [_mapView updateLocationViewWithParam:displayParam];
    
    BMKCoordinateRegion region ;//表示范围的结构体
    region.center = _mapView.centerCoordinate;//中心点
    self.cCoordinate = _mapView.centerCoordinate;//中心点
    region.span.latitudeDelta = 0.002;//经度范围(设置为0.1表示显示范围为0.2的纬度范围)
    region.span.longitudeDelta = 0.002;//纬度范围
    [_mapView setRegion:region animated:YES];
    [self updateClusters];
}

/**
 *地图渲染每一帧画面过程中,以及每次需要重绘地图时(例如添加覆盖物)都会调用此接口
 *@param mapView 地图View
 *@param status 此时地图的状态
 */
- (void)mapView:(BMKMapView *)mapView onDrawMapFrame:(BMKMapStatus *)status {
    if (_clusterZoom != 0 && _clusterZoom != (NSInteger)mapView.zoomLevel) {
        [self updateClusters];
    }
}

3.地图的位置发生变化

- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    //屏幕坐标转地图经纬度
    CLLocationCoordinate2D MapCoordinate = [_mapView convertPoint:_mapView.center toCoordinateFromView:_mapView];
    
    if (_reverseGeoCodeOption==nil) {
        //初始化反地理编码类
        _reverseGeoCodeOption= [[BMKReverseGeoCodeOption alloc] init];
    }
    
    //需要逆地理编码的坐标位置
    _reverseGeoCodeOption.reverseGeoPoint =MapCoordinate;
    [_geoCodeSerch reverseGeoCode:_reverseGeoCodeOption];
   // 如果你是请求自己后台的数据可以在这里请求,可以省去下面检索回来的数据,由于我的是demo,所以下面是必须要的
    BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
    option.pageIndex = 1;
    option.pageCapacity = 10;
    option.location = mapView.centerCoordinate;
    
    option.keyword = @"小吃";
    BOOL flag = [self.poiSearch poiSearchNearBy:option];
    if(flag)
    {
        NSLog(@"周边检索发送成功");
    }
    else
    {
        NSLog(@"周边检索发送失败");
    }
}

// 当地图发生改变之后,检索并
//实现PoiSearchDeleage处理回调结果
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
    }
    else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
        //当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表
        // result.cityList;
        NSLog(@"起始点有歧义");
    } else {
        NSLog(@"抱歉,未找到结果");
    }
    // 清空缓存数据
    [_clusterManager clearClusterItems];
    for (BMKPoiInfo *poiInfo in poiResultList.poiInfoList) {
        XJCluster *cluster = [[XJCluster alloc] init];
        cluster.name = poiInfo.name;
        cluster.pt = poiInfo.pt;
        // 添加数据
        [self addAnnoWithPT:cluster];
    }
}

4.更新点聚合状态

//更新聚合状态
- (void)updateClusters {
 
    _clusterZoom = (NSInteger)_mapView.zoomLevel;
    @synchronized(_clusterCaches) {
        
            NSMutableArray *clusters = [NSMutableArray array];
            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                ///获取聚合后的标注
                __block NSArray *array = [_clusterManager getClusters:_clusterZoom];
        
                dispatch_async(dispatch_get_main_queue(), ^{
                    for (BMKCluster *item in array) {
                        XJClusterAnnotation *annotation = [[XJClusterAnnotation alloc] init];
                        annotation.coordinate = item.coordinate;
                        annotation.size = item.size;
                        annotation.title = item.title;
                        annotation.cluster = item.cluster;
                        [clusters addObject:annotation];
                    }
                    [_mapView removeOverlays:_mapView.overlays];
                    [_mapView removeAnnotations:_mapView.annotations];
                    [_mapView addAnnotations:clusters];

                });
            });
        }
}

效果图:

百度点聚合功能,针头自定义功能_第1张图片
688F5F9E-A891-4D67-96E6-B04458EA8D41.png
demo : https://github.com/SingGitHub/BMKMapClusterView

你可能感兴趣的:(百度点聚合功能,针头自定义功能)