since6.4.0起iOS地图SDK支持海量点图层(BMKMultiPointOverlay)绘制,用于批量展现坐标点数据,并支持点击事件。
NSMutableArray *items = [NSMutableArray array];//读取数据NSData *jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"multPoints" ofType:@"json"]];if (jsonData) {NSArray *array = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];for (NSDictionary *dic in array) {@autoreleasepool {BMKMultiPointItem *item = [[BMKMultiPointItem alloc] init];item.coordinate = CLLocationCoordinate2DMake([dic[@"lat"] floatValue], [dic[@"lng"] floatValue]);[items addObject:item];}}}_multiPointOverlay = [BMKMultiPointOverlay multiPointOverlayWithMultiPointItems:items];/**向地图View添加Overlay,需要实现BMKMapViewDelegate的-mapView:viewForOverlay:方法来生成标注对应的View@param overlay 要添加的overlay*/[_mapView addOverlay:self.multiPointOverlay];
#pragma mark - BMKMapViewDelegate/**根据overlay生成对应的BMKOverlayView@param mapView 地图View@param overlay 指定的overlay@return 生成的覆盖物View*/- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay {if ([overlay isKindOfClass:[BMKMultiPointOverlay class]]) {//初始化一个overlay并返回相应的BMKMultiPointOverlayView的实例BMKMultiPointOverlayView *multiPointOverlayView = [[BMKMultiPointOverlayView alloc] initWithMultiPointOverlay:overlay];multiPointOverlayView.icon = [UIImage imageNamed:@"multPoints"];multiPointOverlayView.anchor = CGPointMake(0.5, 1.0);multiPointOverlayView.pointSize = CGSizeMake(32, 32);MultipointOverlayView.delegate = self;return multiPointOverlayView;}return nil;}
//MARK:BMKMapViewDelegate/**根据overlay生成对应的BMKOverlayView@param mapView 地图View@param overlay 指定的overlay@return 生成的覆盖物View*/- (void)multiPointOverlayView:(BMKMultiPointOverlayView *)overlayView didItemTapped:(BMKMultiPointItem *)item {[self.mapView removeAnnotations:self.mapView.annotations];[self.mapView addAnnotation:item];}
效果如下:
上一篇
下一篇
本篇文章对您是否有帮助?