since6.4.0起ios地图sdk支持海量点图层(bmkmultipointoverlay)绘制,用于批量展现坐标点数据,并支持点击事件。
nsmutablearray*items =[nsmutablearray array];//读取数据nsdata*jsondata =[nsdata datawithcontentsoffile:[[nsbundle mainbundle] pathforresource:@"multpoints"oftype:@"json"]];if(jsondata){nsarray*array =[nsjsonserializationjsonobjectwithdata:jsondata options:0error: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:[bmkmultipointoverlayclass]]){//初始化一个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];}
效果如下:
上一篇
下一篇
本篇文章对您是否有帮助?