[ios3-地图] 如何在iOS地图上高效的显示大量数据 [转]
- typedef struct TBQuadTreeNodeData {
- double x;
- double y;
- void* data;
- } TBQuadTreeNodeData;
- TBQuadTreeNodeData TBQuadTreeNodeDataMake(double x, double y, void* data);
- typedef struct TBBoundingBox {
- double x0; double y0;
- double xf; double yf;
- } TBBoundingBox;
- TBBoundingBox TBBoundingBoxMake(double x0, double y0, double xf, double yf);
- typedef struct quadTreeNode {
- struct quadTreeNode* northWest;
- struct quadTreeNode* northEast;
- struct quadTreeNode* southWest;
- struct quadTreeNode* southEast;
- TBBoundingBox boundingBox;
- int bucketCapacity;
- TBQuadTreeNodeData *points;
- int count;
- } TBQuadTreeNode;
- TBQuadTreeNode* TBQuadTreeNodeMake(TBBoundingBox boundary, int bucketCapacity);
- void TBQuadTreeNodeSubdivide(TBQuadTreeNode* node)
- {
- TBBoundingBox box = node->boundingBox;
- double xMid = (box.xf + box.x0) / 2.0;
- double yMid = (box.yf + box.y0) / 2.0;
- TBBoundingBox northWest = TBBoundingBoxMake(box.x0, box.y0, xMid, yMid);
- node->northWest = TBQuadTreeNodeMake(northWest, node->bucketCapacity);
- TBBoundingBox northEast = TBBoundingBoxMake(xMid, box.y0, box.xf, yMid);
- node->northEast = TBQuadTreeNodeMake(northEast, node->bucketCapacity);
- TBBoundingBox southWest = TBBoundingBoxMake(box.x0, yMid, xMid, box.yf);
- node->southWest = TBQuadTreeNodeMake(southWest, node->bucketCapacity);
- TBBoundingBox southEast = TBBoundingBoxMake(xMid, yMid, box.xf, box.yf);
- node->southEast = TBQuadTreeNodeMake(southEast, node->bucketCapacity);
- }
- bool TBQuadTreeNodeInsertData(TBQuadTreeNode* node, TBQuadTreeNodeData data)
- {
- // Bail if our coordinate is not in the boundingBox
- if (!TBBoundingBoxContainsData(node->boundingBox, data)) {
- return false;
- }
- // Add the coordinate to the points array
- if (node->count < node->bucketCapacity) {
- node->points[node->count++] = data;
- return true;
- }
- // Check to see if the current node is a leaf, if it is, split
- if (node->northWest == NULL) {
- TBQuadTreeNodeSubdivide(node);
- }
- // Traverse the tree
- if (TBQuadTreeNodeInsertData(node->northWest, data)) return true;
- if (TBQuadTreeNodeInsertData(node->northEast, data)) return true;
- if (TBQuadTreeNodeInsertData(node->southWest, data)) return true;
- if (TBQuadTreeNodeInsertData(node->southEast, data)) return true;
- return false;
- }
- typedef void(^TBDataReturnBlock)(TBQuadTreeNodeData data);
- void TBQuadTreeGatherDataInRange(TBQuadTreeNode* node, TBBoundingBox range, TBDataReturnBlock block)
- {
- // If range is not contained in the node's boundingBox then bail
- if (!TBBoundingBoxIntersectsBoundingBox(node->boundingBox, range)) {
- return;
- }
- for (int i = 0; i < node->count; i++) {
- // Gather points contained in range
- if (TBBoundingBoxContainsData(range, node->points[i])) {
- block(node->points[i]);
- }
- }
- // Bail if node is leaf
- if (node->northWest == NULL) {
- return;
- }
- // Otherwise traverse down the tree
- TBQuadTreeGatherDataInRange(node->northWest, range, block);
- TBQuadTreeGatherDataInRange(node->northEast, range, block);
- TBQuadTreeGatherDataInRange(node->southWest, range, block);
- TBQuadTreeGatherDataInRange(node->southEast, range, block);
- }
- typedef struct TBHotelInfo {
- char* hotelName;
- char* hotelPhoneNumber;
- } TBHotelInfo;
- TBQuadTreeNodeData TBDataFromLine(NSString *line)
- {
- // Example line:
- // -80.26262, 25.81015, Everglades Motel, USA-United States, +1 305-888-8797
- NSArray *components = [line componentsSeparatedByString:@","];
- double latitude = [components[1] doubleValue];
- double longitude = [components[0] doubleValue];
- TBHotelInfo* hotelInfo = malloc(sizeof(TBHotelInfo));
- NSString *hotelName = [components[2] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
- hotelInfo->hotelName = malloc(sizeof(char) * hotelName.length + 1);
- strncpy(hotelInfo->hotelName, [hotelName UTF8String], hotelName.length + 1);
- NSString *hotelPhoneNumber = [[components lastObject] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
- hotelInfo->hotelPhoneNumber = malloc(sizeof(char) * hotelPhoneNumber.length + 1);
- strncpy(hotelInfo->hotelPhoneNumber, [hotelPhoneNumber UTF8String], hotelPhoneNumber.length + 1);
- return TBQuadTreeNodeDataMake(latitude, longitude, hotelInfo);
- }
- - (void)buildTree
- {
- NSString *data = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"USA-HotelMotel" ofType:@"csv"] encoding:NSASCIIStringEncoding error:nil];
- NSArray *lines = [data componentsSeparatedByString:@"\n"];
- NSInteger count = lines.count - 1;
- TBQuadTreeNodeData *dataArray = malloc(sizeof(TBQuadTreeNodeData) * count);
- for (NSInteger i = 0; i < count; i++) {
- dataArray[i] = TBDataFromLine(lines[i]);
- }
- TBBoundingBox world = TBBoundingBoxMake(19, -166, 72, -53);
- _root = TBQuadTreeBuildWithData(dataArray, count, world, 4);
- }
- - (NSArray *)clusteredAnnotationsWithinMapRect:(MKMapRect)rect withZoomScale:(double)zoomScale
- {
- double TBCellSize = TBCellSizeForZoomScale(zoomScale);
- double scaleFactor = zoomScale / TBCellSize;
- NSInteger minX = floor(MKMapRectGetMinX(rect) * scaleFactor);
- NSInteger maxX = floor(MKMapRectGetMaxX(rect) * scaleFactor);
- NSInteger minY = floor(MKMapRectGetMinY(rect) * scaleFactor);
- NSInteger maxY = floor(MKMapRectGetMaxY(rect) * scaleFactor);
- NSMutableArray *clusteredAnnotations = [[NSMutableArray alloc] init];
- for (NSInteger x = minX; x <= maxX; x++) {
- for (NSInteger y = minY; y <= maxY; y++) {
- MKMapRect mapRect = MKMapRectMake(x / scaleFactor, y / scaleFactor, 1.0 / scaleFactor, 1.0 / scaleFactor);
- __block double totalX = 0;
- __block double totalY = 0;
- __block int count = 0;
- TBQuadTreeGatherDataInRange(self.root, TBBoundingBoxForMapRect(mapRect), ^(TBQuadTreeNodeData data) {
- totalX += data.x;
- totalY += data.y;
- count++;
- });
- if (count >= 1) {
- CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(totalX / count, totalY / count);
- TBClusterAnnotation *annotation = [[TBClusterAnnotation alloc] initWithCoordinate:coordinate count:count];
- [clusteredAnnotations addObject:annotation];
- }
- }
- }
- return [NSArray arrayWithArray:clusteredAnnotations];
- }
- - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
- {
- [[NSOperationQueue new] addOperationWithBlock:^{
- double zoomScale = self.mapView.bounds.size.width / self.mapView.visibleMapRect.size.width;
- NSArray *annotations = [self.coordinateQuadTree clusteredAnnotationsWithinMapRect:mapView.visibleMapRect withZoomScale:zoomScale];
- [self updateMapViewAnnotationsWithAnnotations:annotations];
- }];
- }
- - (void)updateMapViewAnnotationsWithAnnotations:(NSArray *)annotations
- {
- NSMutableSet *before = [NSMutableSet setWithArray:self.mapView.annotations];
- NSSet *after = [NSSet setWithArray:annotations];
- // Annotations circled in blue shared by both sets
- NSMutableSet *toKeep = [NSMutableSet setWithSet:before];
- [toKeep intersectSet:after];
- // Annotations circled in green
- NSMutableSet *toAdd = [NSMutableSet setWithSet:after];
- [toAdd minusSet:toKeep];
- // Annotations circled in red
- NSMutableSet *toRemove = [NSMutableSet setWithSet:before];
- [toRemove minusSet:after];
- // These two methods must be called on the main thread
- [[NSOperationQueue mainQueue] addOperationWithBlock:^{
- [self.mapView addAnnotations:[toAdd allObjects]];
- [self.mapView removeAnnotations:[toRemove allObjects]];
- }];
- }
- static CGFloat const TBScaleFactorAlpha = 0.3;
- static CGFloat const TBScaleFactorBeta = 0.4;
- CGFloat TBScaledValueForValue(CGFloat value)
- {
- return 1.0 / (1.0 + expf(-1 * TBScaleFactorAlpha * powf(value, TBScaleFactorBeta)));
- }
- - (void)setCount:(NSUInteger)count
- {
- _count = count;
- // Our max size is (44,44)
- CGRect newBounds = CGRectMake(0, 0, roundf(44 * TBScaledValueForValue(count)), roundf(44 * TBScaledValueForValue(count)));
- self.frame = TBCenterRect(newBounds, self.center);
- CGRect newLabelBounds = CGRectMake(0, 0, newBounds.size.width / 1.3, newBounds.size.height / 1.3);
- self.countLabel.frame = TBCenterRect(newLabelBounds, TBRectCenter(newBounds));
- self.countLabel.text = [@(_count) stringValue];
- [self setNeedsDisplay];
- }
- - (void)setupLabel
- {
- _countLabel = [[UILabel alloc] initWithFrame:self.frame];
- _countLabel.backgroundColor = [UIColor clearColor];
- _countLabel.textColor = [UIColor whiteColor];
- _countLabel.textAlignment = NSTextAlignmentCenter;
- _countLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.75];
- _countLabel.shadowOffset = CGSizeMake(0, -1);
- _countLabel.adjustsFontSizeToFitWidth = YES;
- _countLabel.numberOfLines = 1;
- _countLabel.font = [UIFont boldSystemFontOfSize:12];
- _countLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
- [self addSubview:_countLabel];
- }
- - (void)drawRect:(CGRect)rect
- {
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSetAllowsAntialiasing(context, true);
- UIColor *outerCircleStrokeColor = [UIColor colorWithWhite:0 alpha:0.25];
- UIColor *innerCircleStrokeColor = [UIColor whiteColor];
- UIColor *innerCircleFillColor = [UIColor colorWithRed:(255.0 / 255.0) green:(95 / 255.0) blue:(42 / 255.0) alpha:1.0];
- CGRect circleFrame = CGRectInset(rect, 4, 4);
- [outerCircleStrokeColor setStroke];
- CGContextSetLineWidth(context, 5.0);
- CGContextStrokeEllipseInRect(context, circleFrame);
- [innerCircleStrokeColor setStroke];
- CGContextSetLineWidth(context, 4);
- CGContextStrokeEllipseInRect(context, circleFrame);
- [innerCircleFillColor setFill];
- CGContextFillEllipseInRect(context, circleFrame);
- }
- - (void)addBounceAnnimationToView:(UIView *)view
- {
- CAKeyframeAnimation *bounceAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
- bounceAnimation.values = @[@(0.05), @(1.1), @(0.9), @(1)];
- bounceAnimation.duration = 0.6;
- NSMutableArray *timingFunctions = [[NSMutableArray alloc] init];
- for (NSInteger i = 0; i < 4; i++) {
- [timingFunctions addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
- }
- [bounceAnimation setTimingFunctions:timingFunctions.copy];
- bounceAnimation.removedOnCompletion = NO;
- [view.layer addAnimation:bounceAnimation forKey:@"bounce"];
- }
- - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
- {
- for (UIView *view in views) {
- [self addBounceAnnimationToView:view];
- }
- }
- NSInteger TBZoomScaleToZoomLevel(MKZoomScale scale)
- {
- double totalTilesAtMaxZoom = MKMapSizeWorld.width / 256.0;
- NSInteger zoomLevelAtMaxZoom = log2(totalTilesAtMaxZoom);
- NSInteger zoomLevel = MAX(0, zoomLevelAtMaxZoom + floor(log2f(scale) + 0.5));
- return zoomLevel;
- }
- float TBCellSizeForZoomScale(MKZoomScale zoomScale)
- {
- NSInteger zoomLevel = TBZoomScaleToZoomLevel(zoomScale);
- switch (zoomLevel) {
- case 13:
- case 14:
- case 15:
- return 64;
- case 16:
- case 17:
- case 18:
- return 32;
- case 19:
- return 16;
- default:
- return 88;
- }
- }
[ios3-地图] 如何在iOS地图上高效的显示大量数据 [转]的更多相关文章
- 如何在iOS地图上高效的显示大量数据
2016-01-13 / 23:02:13 刚才在微信上看到这篇由cocoachina翻译小组成员翻译的文章,觉得还是挺值得参考的,因此转载至此,原文请移步:http://robots.thought ...
- 如何在iOS手机上进行自动化测试
版权声明:允许转载,但转载必须保留原链接:请勿用作商业或者非法用途 Airtest支持iOS自动化测试,在Mac上为iOS手机部署iOS-Tagent之后,就可以使用AirtestIDE连接设备,像连 ...
- fir.im Weekly - 如何在 iOS 上构建 TensorFlow 应用
本期 fir.im Weekly 收集了最近新鲜出炉的 iOS /Android 技术分享,包括 iOS 系统开发 TensorFlow 教程.iOS 新架构.iOS Notifications 推送 ...
- iOS 地图定位及大头针的基本使用
地图 Part1 - 定位及大头针的基本使用 一.MapKit 作用 : 用于地图展示 如大头针,路线,覆盖层展示等(着重界面展示) 使用步骤 导入头文件 #import <MapKit/Map ...
- 【高德API】如何利用MapKit开发全英文检索的iOS地图
原文:[高德API]如何利用MapKit开发全英文检索的iOS地图 制作全英文地图的展示并不困难,但是要制作全英文的数据检索列表,全英文的信息窗口,你就没办法了吧.告诉你,我有妙招!使用iOS自带的M ...
- 【iOS地图开发】巧妙打造中英文全球地图
地图开发的同学们经常遇到这样的问题,国内版地图开发,用高德或者百度就行了.但是,国外的地图怎么办?这里告诉大家,如果利用iOS地图,打造中英文的,国内国外都能用的,全球地图. 制作全英文地图的展示并不 ...
- iOS 地图相关
参考博文:https://blog.csdn.net/zhengang007/article/details/52858198?utm_source=blogxgwz7 1.坐标系 目前常见的坐标系有 ...
- Swift - 使用MapKit显示地图,并在地图上做标记
通过使用MapKit可以将地图嵌入到视图中,MapKit框架除了可以显示地图,还支持在地图上做标记. 1,通过mapType属性,可以设置地图的显示类型 MKMapType.Standard :标准地 ...
- 从底层谈WebGIS 原理设计与实现(六):WebGIS中地图瓦片在Canvas上的拼接显示原理
从底层谈WebGIS 原理设计与实现(六):WebGIS中地图瓦片在Canvas上的拼接显示原理 作者:naaoveGI… 文章来源:naaoveGIS 点击数:1145 更新时间: ...
随机推荐
- Android单元测试Junit (一)
1.在eclips中建立一个Android工程,具体信息如下: 2.配置单元测试环境,打开AndroidManifest.xml,具体代码如下所示: <?xml version="1. ...
- 【蜗牛—漫漫IT路之大学篇(九)
】
再来一篇叨叨的博客 近期,状态还是那个状态,人还是那个人. 前两天,感冒了,可能是宿舍阴面的事吧.然后,中午睡觉的时候穿着短袖披了一件外套,然后鼻子就不通气了.只是,前天晚上,我骑着崔国强的车子跑了不 ...
- js预解析问题总结
//示例 1 alert(a) // undefind. alert(fn) // function 整个函数块. var a = 1; function fn(){ return falss; }; ...
- struts2 <s: select 标签值
JSP页面: <s:select label="家长导航" value="id" name="navson.pid" list=&q ...
- SpringMVC格式化显示
SpringMVC学习系列(7) 之 格式化显示 在系列(6)中我们介绍了如何验证提交的数据的正确性,当数据验证通过后就会被我们保存起来.保存的数据会用于以后的展示,这才是保存的价值.那么在展示的时候 ...
- C#的Task和Java的Future
C#的Task和Java的Future 自从项目中语言换成Java后就很久没有看C#了,但说实话我是身在曹营心在汉啊.早就知道.NET4.5新增了async和await但一直没有用过,今天看到这篇文章 ...
- Orchard中的Host和Tenant
Orchard的多个子站点特性 Orchard中可以支持多个子站点.当你运行Orchard的时候,通常一个网站运行在一个应用程序域中.这也是一般ASP.NET应用程序区分两个网站的方法,也就是说两 ...
- 自然语言处理(NLP)常用开源工具总结(转)
..................................内容纯转发+收藏................................... 学习自然语言这一段时间以来接触和听说了好多开 ...
- 多模块分布式系统的简单服务访问 - OSGI原形(.NET)
多模块分布式系统的简单服务访问 - OSGI原形(.NET) 先描述一下本篇描述的适用场景(3台server, 各个模块分布在各个Server上,分布式模块互相依赖.交互的场景): 多个OSIG引擎交 ...
- Redis系统学习 一、基础知识
1.数据库 select 1 select 0 2.命令.关键字和值 redis不仅仅是一种简单的关键字-值型存储,从其核心概念来看,Redsi的5种数据结构中的每一个都至少有一个关键字和一个值.在 ...