android德地图点聚合,点聚合 | 百度地图API SDK

/** 更新标注展示. */

- (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];

});

});

}

}

}

#pragma mark - BMKMapViewDelegate

/** 根据anntation生成对应的View. */

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id )annotation {

ClusterAnnotation *cluster = (ClusterAnnotation*)annotation;

BMKPinAnnotationView *annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewIdentifier];

UILabel *annotationLabel = [[UILabel alloc] init];

annotationLabel.textColor = [UIColor whiteColor];

annotationLabel.font = [UIFont systemFontOfSize:11];

annotationLabel.textAlignment = NSTextAlignmentCenter;

annotationLabel.hidden = NO;

annotationLabel.text = [NSString stringWithFormat:@"%ld", (long)cluster.size];

annotationLabel.backgroundColor = [UIColor greenColor];

annotationView.alpha = 0.8;

[annotationView addSubview:annotationLabel];

// 此处为了方便展示使用了简易的大头针标注,开发者可以根据自身需求来自定义标注,圆角设置建议使用圆形图片

if (cluster.size == 1) {

annotationLabel.hidden = YES;

annotationView.pinColor = BMKPinAnnotationColorRed;

}

if (cluster.size >= 500) {

annotationLabel.frame = CGRectMake(0, 0, 100, 100);

annotationLabel.layer.cornerRadius = 50;

annotationLabel.layer.masksToBounds = YES;

annotationLabel.backgroundColor = [UIColor redColor];

[annotationView setBounds:CGRectMake(0, 0, 100, 100)];

} else if (cluster.size >= 200 && cluster.size < 500) {

annotationLabel.frame = CGRectMake(0, 0, 80, 80);

annotationLabel.layer.cornerRadius = 40;

annotationLabel.layer.masksToBounds = YES;

annotationLabel.backgroundColor = [UIColor purpleColor];

[annotationView setBounds:CGRectMake(0, 0, 80, 80)];

} else if (cluster.size > 50) {

annotationLabel.frame = CGRectMake(0, 0, 60, 60);

annotationLabel.layer.cornerRadius = 30;

annotationLabel.layer.masksToBounds = YES;

annotationLabel.backgroundColor = [UIColor orangeColor];

[annotationView setBounds:CGRectMake(0, 0, 60, 60)];

} else if (cluster.size > 20) {

annotationLabel.frame = CGRectMake(0, 0, 40, 40);

annotationLabel.layer.cornerRadius = 20;

annotationLabel.layer.masksToBounds = YES;

annotationLabel.backgroundColor = [UIColor blueColor];

[annotationView setBounds:CGRectMake(0, 0, 40, 40)];

} else {

annotationLabel.frame = CGRectMake(0, 0, 20, 20);

annotationLabel.layer.cornerRadius = 10;

annotationLabel.layer.masksToBounds = YES;

annotationLabel.backgroundColor = [UIColor greenColor];

[annotationView setBounds:CGRectMake(0, 0, 20, 20)];

}

annotationView.draggable = YES;

annotationView.annotation = annotation;

return annotationView;

}

/** 当点击annotationView弹出的泡泡时,回调此方法. */

- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view {

if ([view isKindOfClass:[BMKPinAnnotationView class]]) {

ClusterAnnotation *clusterAnnotation = (ClusterAnnotation*)view.annotation;

if (clusterAnnotation.size > 1) {

/**

设定地图中心点坐标

@param coordinate 要设定的地图中心点坐标,用经纬度表示

@param animated 是否采用动画效果

*/

[mapView setCenterCoordinate:view.annotation.coordinate];

/**

放大一级比例尺

@return 是否成功

*/

[mapView zoomIn];

}

}

}

/** 地图加载完成时会调用此方法. */

- (void)mapViewDidFinishLoading:(BMKMapView *)mapView {

[self updateClusters];

}

/** 地图渲染每一帧画面过程中,以及每次需要重新绘制地图时(例如添加覆盖物)都会调用此方法*/

- (void)mapView:(BMKMapView *)mapView onDrawMapFrame:(BMKMapStatus *)status {

if (_clusterZoom != 0 && _clusterZoom != (NSInteger)mapView.zoomLevel) {

[self updateClusters];

}

}

复制

深色

复制成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值