基本功能
- 地图
百度地图的基本使用和苹果自带的Mapkit差不多,很多方法都是类似的 - 定位
iOS9之后定位权限只有系统才可以调用,百度SDK后台实际是通过苹果coreLocation框架进行定位。
聚合功能
该功能比较新鲜,进行了初体验。要使用该功能必须要手动导入BMKCluster相关文件,并不在百度SDK开发包内。
- 初始化并模拟添加20个标注点
_clusterManager = [[BMKClusterManager alloc] init];
CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(27.986333, 120.690356);
//向点聚合管理类中添加标注
for (NSInteger i = 0; i < 20; i++) {
double lat = (arc4random() % 100) * 0.001f;
double lon = (arc4random() % 100) * 0.001f;
BMKClusterItem *clusterItem = [[BMKClusterItem alloc] init];
clusterItem.coor = CLLocationCoordinate2DMake(coor.latitude + lat, coor.longitude + lon);
[_clusterManager addClusterItem:clusterItem];
}
- 获取聚合后的标注
///获取聚合后的标注
__block NSArray *array = [_clusterManager getClusters:_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:@"我是%zd个", item.size];
[clusters addObject:annotation];
}
[_mapView removeAnnotations:_mapView.annotations];
[_mapView addAnnotations:clusters];
});
- 根据anntation生成对应的View
// 根据anntation生成对应的View
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
//普通annotation
NSString *AnnotationViewID = @"ClusterMark";
ClusterAnnotation *cluster = (ClusterAnnotation*)annotation;
ClusterAnnotationView *annotationView = [[ClusterAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
annotationView.size = cluster.size;
annotationView.draggable = YES;
annotationView.annotation = cluster;
return annotationView;
}
演示效果: