Fetch the repository succeeded.
提供iOS版本在地图上显示大量poi点的时候,进行点聚合的功能。
self.coordinateQuadTree = [[CoordinateQuadTree alloc] init];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
/* 建立四叉树. */
[self.coordinateQuadTree buildTreeWithPOIs:respons.pois];
dispatch_async(dispatch_get_main_queue(), ^{
/* 计算当前mapView区域内需要显示的annotation. */
NSLog(@"First time calculate annotations.");
[self addAnnotationsToMapView:self.mapView];
});
});
- (void)addAnnotationsToMapView:(MAMapView *)mapView
{
/* 判断是否已建树. */
if (self.coordinateQuadTree.root == nil)
{
return;
}
/* 根据当前zoomLevel和zoomScale 进行annotation聚合. */
double zoomScale = self.mapView.bounds.size.width / self.mapView.visibleMapRect.size.width;
/* 基于先前建立的四叉树索引,计算当前需要显示的annotations. */
NSArray *annotations = [self.coordinateQuadTree clusteredAnnotationsWithinMapRect:mapView.visibleMapRect
withZoomScale:zoomScale
andZoomLevel:mapView.zoomLevel];
/* 更新annotations. */
[self updateMapViewAnnotationsWithAnnotations:annotations];
}
/* 更新annotation. */
- (void)updateMapViewAnnotationsWithAnnotations:(NSArray *)annotations
{
/* 用户滑动时,保留仍然可用的标注,去除屏幕外标注,添加新增区域的标注 */
NSMutableSet *before = [NSMutableSet setWithArray:self.mapView.annotations];
[before removeObject:[self.mapView userLocation]];
NSSet *after = [NSSet setWithArray:annotations];
/* 保留仍然位于屏幕内的annotation. */
NSMutableSet *toKeep = [NSMutableSet setWithSet:before];
[toKeep intersectSet:after];
/* 需要添加的annotation. */
NSMutableSet *toAdd = [NSMutableSet setWithSet:after];
[toAdd minusSet:toKeep];
/* 删除位于屏幕外的annotation. */
NSMutableSet *toRemove = [NSMutableSet setWithSet:before];
[toRemove minusSet:after];
/* 更新. */
dispatch_async(dispatch_get_main_queue(), ^{
[self.mapView addAnnotations:[toAdd allObjects]];
[self.mapView removeAnnotations:[toRemove allObjects]];
});
}
-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
if ([annotation isKindOfClass:[ClusterAnnotation class]])
{
/* dequeue重用annotationView代码. */
/* ... */
/* 设置annotationView的属性. */
annotationView.annotation = annotation;
annotationView.count = [(ClusterAnnotation *)annotation count];
/* 设置annotationView的callout属性和calloutView代码. */
/* ... */
return annotationView;
}
return nil;
}
- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
/* mapView区域变化时重算annotation. */
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self addAnnotationsToMapView:self.mapView];
});
}
<UIViewController>
BaseMapViewController
地图基类
AnnotationClusterViewController
poi点聚合PoiDetailViewController
显示poi详细信息列表MAAnnotationView
ClusterAnnotationView
自定义的聚合annotationViewConform to <MAAnnotation>
ClusterAnnotation
记录annotation的信息,如其代表的poi数组、poi的个数、poi平均坐标,并提供两个annotation是否Equal的判断CoordinateQuadTree
封装的四叉树类QuadTree
四叉树基本算法手机扫描如下二维码直接安装
手机上打开地址:<http://fir.im/cademo>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
Activity
Community
Health
Trend
Influence
:Code submit frequency
:React/respond to issue & PR etc.
:Well-balanced team members and collaboration
:Recent popularity of project
:Star counts, download counts etc.