iOS中的MapKit集成了google地图api的很多功能加上iOS的定位的功能,我们就可以实现将你运行的轨迹绘制到地图上面。这个功能非常有用,比如快递追踪、汽车的gprs追踪、人员追踪等等。这篇文章我们将使用Map Kit和iOS的定位功能,将你的运行轨迹绘制在地图上面。

实现

在之前的一篇文章描述了如何在地图上显示自己的位置,如果我们将这些位置先保存起来,然后串联起来绘制到地图上面,那就是我们的运行轨迹了。

首先我们看下如何在地图上绘制曲线。在Map Kit中提供了一个叫MKPolyline的类,我们可以利用它来绘制曲线,先看个简单的例子。

使用下面代码从一个文件中读取出经纬度,然后创建一个路径:MKPolyline实例。

[plain] view plaincopy
  1. -(void) loadRoute
  2. {
  3. NSString* filePath = [[NSBundle mainBundle] pathForResource:@”route” ofType:@”csv”];
  4. NSString* fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
  5. NSArray* pointStrings = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  6. // while we create the route points, we will also be calculating the bounding box of our route
  7. // so we can easily zoom in on it.
  8. MKMapPoint northEastPoint;
  9. MKMapPoint southWestPoint;
  10. // create a c array of points.
  11. MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * pointStrings.count);
  12. for(int idx = 0; idx < pointStrings.count; idx++)
  13. {
  14. // break the string down even further to latitude and longitude fields.
  15. NSString* currentPointString = [pointStrings objectAtIndex:idx];
  16. NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
  17. CLLocationDegrees latitude = [[latLonArr objectAtIndex:0] doubleValue];
  18. CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue];
  19. // create our coordinate and add it to the correct spot in the array
  20. CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
  21. MKMapPoint point = MKMapPointForCoordinate(coordinate);
  22. //
  23. // adjust the bounding box
  24. //
  25. // if it is the first point, just use them, since we have nothing to compare to yet.
  26. if (idx == 0) {
  27. northEastPoint = point;
  28. southWestPoint = point;
  29. }
  30. else
  31. {
  32. if (point.x > northEastPoint.x)
  33. northEastPoint.x = point.x;
  34. if(point.y > northEastPoint.y)
  35. northEastPoint.y = point.y;
  36. if (point.x < southWestPoint.x)
  37. southWestPoint.x = point.x;
  38. if (point.y < southWestPoint.y)
  39. southWestPoint.y = point.y;
  40. }
  41. pointArr[idx] = point;
  42. }
  43. // create the polyline based on the array of points.
  44. self.routeLine = [MKPolyline polylineWithPoints:pointArr count:pointStrings.count];
  45. _routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
  46. // clear the memory allocated earlier for the points
  47. free(pointArr);
  48. }
[plain] view plaincopy
  1. -(void) loadRoute
  2. {
  3. NSString* filePath = [[NSBundle mainBundle] pathForResource:@”route” ofType:@”csv”];
  4. NSString* fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
  5. NSArray* pointStrings = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  6. // while we create the route points, we will also be calculating the bounding box of our route
  7. // so we can easily zoom in on it.
  8. MKMapPoint northEastPoint;
  9. MKMapPoint southWestPoint;
  10. // create a c array of points.
  11. MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * pointStrings.count);
  12. for(int idx = 0; idx < pointStrings.count; idx++)
  13. {
  14. // break the string down even further to latitude and longitude fields.
  15. NSString* currentPointString = [pointStrings objectAtIndex:idx];
  16. NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
  17. CLLocationDegrees latitude = [[latLonArr objectAtIndex:0] doubleValue];
  18. CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue];
  19. // create our coordinate and add it to the correct spot in the array
  20. CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
  21. MKMapPoint point = MKMapPointForCoordinate(coordinate);
  22. //
  23. // adjust the bounding box
  24. //
  25. // if it is the first point, just use them, since we have nothing to compare to yet.
  26. if (idx == 0) {
  27. northEastPoint = point;
  28. southWestPoint = point;
  29. }
  30. else
  31. {
  32. if (point.x > northEastPoint.x)
  33. northEastPoint.x = point.x;
  34. if(point.y > northEastPoint.y)
  35. northEastPoint.y = point.y;
  36. if (point.x < southWestPoint.x)
  37. southWestPoint.x = point.x;
  38. if (point.y < southWestPoint.y)
  39. southWestPoint.y = point.y;
  40. }
  41. pointArr[idx] = point;
  42. }
  43. // create the polyline based on the array of points.
  44. self.routeLine = [MKPolyline polylineWithPoints:pointArr count:pointStrings.count];
  45. _routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
  46. // clear the memory allocated earlier for the points
  47. free(pointArr);
  48. }

将这个路径添加到地图上

[plain] view plaincopy
  1. [self.mapView addOverlay:self.routeLine];
[plain] view plaincopy
  1. [self.mapView addOverlay:self.routeLine];

显示在地图上:

[plain] view plaincopy
  1. - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id )overlay
  2. {
  3. MKOverlayView* overlayView = nil;
  4. if(overlay == self.routeLine)
  5. {
  6. //if we have not yet created an overlay view for this overlay, create it now.
  7. if(nil == self.routeLineView)
  8. {
  9. self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
  10. self.routeLineView.fillColor = [UIColor redColor];
  11. self.routeLineView.strokeColor = [UIColor redColor];
  12. self.routeLineView.lineWidth = 3;
  13. }
  14. overlayView = self.routeLineView;
  15. }
  16. return overlayView;
  17. }
[plain] view plaincopy
  1. - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id )overlay
  2. {
  3. MKOverlayView* overlayView = nil;
  4. if(overlay == self.routeLine)
  5. {
  6. //if we have not yet created an overlay view for this overlay, create it now.
  7. if(nil == self.routeLineView)
  8. {
  9. self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
  10. self.routeLineView.fillColor = [UIColor redColor];
  11. self.routeLineView.strokeColor = [UIColor redColor];
  12. self.routeLineView.lineWidth = 3;
  13. }
  14. overlayView = self.routeLineView;
  15. }
  16. return overlayView;
  17. }

效果:

然后我们在从文件中读取位置的方法改成从用gprs等方法获取当前位置。

第一步:创建一个CLLocationManager实例
第二步:设置CLLocationManager实例委托和精度
第三步:设置距离筛选器distanceFilter
第四步:启动请求
代码如下:

[plain] view plaincopy
  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. noUpdates = 0;
  4. locations = [[NSMutableArray alloc] init];
  5. locationMgr = [[CLLocationManager alloc] init];
  6. locationMgr.delegate = self;
  7. locationMgr.desiredAccuracy =kCLLocationAccuracyBest;
  8. locationMgr.distanceFilter  = 1.0f;
  9. [locationMgr startUpdatingLocation];
  10. }
[plain] view plaincopy
  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. noUpdates = 0;
  4. locations = [[NSMutableArray alloc] init];
  5. locationMgr = [[CLLocationManager alloc] init];
  6. locationMgr.delegate = self;
  7. locationMgr.desiredAccuracy =kCLLocationAccuracyBest;
  8. locationMgr.distanceFilter  = 1.0f;
  9. [locationMgr startUpdatingLocation];
  10. }

上面的代码我定义了一个数组,用于保存运行轨迹的经纬度。

每次通知更新当前位置的时候,我们将当前位置的经纬度放到这个数组中,并重新绘制路径,代码如下:

[plain] view plaincopy
  1. - (void)locationManager:(CLLocationManager *)manager
  2. didUpdateToLocation:(CLLocation *)newLocation
  3. fromLocation:(CLLocation *)oldLocation{
  4. noUpdates++;
  5. [locations addObject: [NSString stringWithFormat:@"%f,%f",[newLocation coordinate].latitude, [newLocation coordinate].longitude]];
  6. [self updateLocation];
  7. if (self.routeLine!=nil) {
  8. self.routeLine =nil;
  9. }
  10. if(self.routeLine!=nil)
  11. [self.mapView removeOverlay:self.routeLine];
  12. self.routeLine =nil;
  13. // create the overlay
  14. [self loadRoute];
  15. // add the overlay to the map
  16. if (nil != self.routeLine) {
  17. [self.mapView addOverlay:self.routeLine];
  18. }
  19. // zoom in on the route.
  20. [self zoomInOnRoute];
  21. }
[plain] view plaincopy
  1. - (void)locationManager:(CLLocationManager *)manager
  2. didUpdateToLocation:(CLLocation *)newLocation
  3. fromLocation:(CLLocation *)oldLocation{
  4. noUpdates++;
  5. [locations addObject: [NSString stringWithFormat:@"%f,%f",[newLocation coordinate].latitude, [newLocation coordinate].longitude]];
  6. [self updateLocation];
  7. if (self.routeLine!=nil) {
  8. self.routeLine =nil;
  9. }
  10. if(self.routeLine!=nil)
  11. [self.mapView removeOverlay:self.routeLine];
  12. self.routeLine =nil;
  13. // create the overlay
  14. [self loadRoute];
  15. // add the overlay to the map
  16. if (nil != self.routeLine) {
  17. [self.mapView addOverlay:self.routeLine];
  18. }
  19. // zoom in on the route.
  20. [self zoomInOnRoute];
  21. }

我们将前面从文件获取经纬度创建轨迹的代码修改成从这个数组中取值就行了:

[plain] view plaincopy
  1. // creates the route (MKPolyline) overlay
  2. -(void) loadRoute
  3. {
  4. // while we create the route points, we will also be calculating the bounding box of our route
  5. // so we can easily zoom in on it.
  6. MKMapPoint northEastPoint;
  7. MKMapPoint southWestPoint;
  8. // create a c array of points.
  9. MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * locations.count);
  10. for(int idx = 0; idx < locations.count; idx++)
  11. {
  12. // break the string down even further to latitude and longitude fields.
  13. NSString* currentPointString = [locations objectAtIndex:idx];
  14. NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
  15. CLLocationDegrees latitude  = [[latLonArr objectAtIndex:0] doubleValue];
  16. CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue];
  17. CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
  18. MKMapPoint point = MKMapPointForCoordinate(coordinate);
  19. if (idx == 0) {
  20. northEastPoint = point;
  21. southWestPoint = point;
  22. }
  23. else
  24. {
  25. if (point.x > northEastPoint.x)
  26. northEastPoint.x = point.x;
  27. if(point.y > northEastPoint.y)
  28. northEastPoint.y = point.y;
  29. if (point.x < southWestPoint.x)
  30. southWestPoint.x = point.x;
  31. if (point.y < southWestPoint.y)
  32. southWestPoint.y = point.y;
  33. }
  34. pointArr[idx] = point;
  35. }
  36. self.routeLine = [MKPolyline polylineWithPoints:pointArr count:locations.count];
  37. _routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
  38. free(pointArr);
  39. }
[plain] view plaincopy
  1. // creates the route (MKPolyline) overlay
  2. -(void) loadRoute
  3. {
  4. // while we create the route points, we will also be calculating the bounding box of our route
  5. // so we can easily zoom in on it.
  6. MKMapPoint northEastPoint;
  7. MKMapPoint southWestPoint;
  8. // create a c array of points.
  9. MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * locations.count);
  10. for(int idx = 0; idx < locations.count; idx++)
  11. {
  12. // break the string down even further to latitude and longitude fields.
  13. NSString* currentPointString = [locations objectAtIndex:idx];
  14. NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
  15. CLLocationDegrees latitude  = [[latLonArr objectAtIndex:0] doubleValue];
  16. CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue];
  17. CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
  18. MKMapPoint point = MKMapPointForCoordinate(coordinate);
  19. if (idx == 0) {
  20. northEastPoint = point;
  21. southWestPoint = point;
  22. }
  23. else
  24. {
  25. if (point.x > northEastPoint.x)
  26. northEastPoint.x = point.x;
  27. if(point.y > northEastPoint.y)
  28. northEastPoint.y = point.y;
  29. if (point.x < southWestPoint.x)
  30. southWestPoint.x = point.x;
  31. if (point.y < southWestPoint.y)
  32. southWestPoint.y = point.y;
  33. }
  34. pointArr[idx] = point;
  35. }
  36. self.routeLine = [MKPolyline polylineWithPoints:pointArr count:locations.count];
  37. _routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);
  38. free(pointArr);
  39. }

这样我们就将我们运行得轨迹绘制google地图上面了。

扩展:

如果你想使用其他的地图,比如百度地图,其实也很方便。可以将百度地图放置到UIWebView中间,通过用js去绘制轨迹。

总结:这篇文章我们介绍了一种常见的技术实现:在地图上绘制出你运行的轨迹。

iOS 在地图上绘制运动轨迹相关推荐

  1. iOS 在地图上绘制出你运动的轨迹

    iOS中的MapKit集成了google地图api的很多功能加上iOS的定位的功能,我们就可以实现将你运行的轨迹绘制到地图上面.这个功能非常有用,比如快递追踪.汽车的gprs追踪.人员追踪等等.这篇文 ...

  2. 安卓开发:怎样实时在百度地图上绘制运动轨迹?

    我的设想是这样的:每5秒将gps传来的值传入一个list里面,然后又立刻读取list中的值,然后将值作为参数 传入划线方法中.但是这样就出现个问题,list的大小未知.反正最后运行也没出现轨迹. 有没 ...

  3. iOS开发之在地图上绘制出你运行的轨迹

    iOS中的MapKit集成了google地图api的很多功能加上iOS的定位的功能,我们就可以实现将你运行的轨迹绘制到地图上面.这个功能非常有 用,比如汽车的gprs追踪.人员追踪.快递追踪等等.这篇 ...

  4. iOS开发之在地图上绘制出你运动的轨迹

    iOS中的MapKit集成了google地图api的很多功能加上iOS的定位的功能,我们就可以实现将你运行的轨迹绘制到地图上面.这个功能非常有用,比如快递追踪.汽车的gprs追踪.人员追踪等等.这篇文 ...

  5. java echarts 散点图,echarts在地图上绘制散点图(任意点)

    项目需求:在省份地图上绘制散点图,散点位置不一定是哪个城市或哪个区县,即任意点 通过查询官网文档,找到一个与需求类似的Demo:https://www.echartsjs.com/gallery/ed ...

  6. android百度地图轨迹实现,android 获取GPS经纬度在百度地图上绘制轨迹

    实现将一组GPS模块获取的经纬度数据在百度地图上绘制轨迹 1.将经纬度转换成百度地图坐标 /** * 标准的GPS经纬度坐标直接在地图上绘制会有偏移,这是测绘局和地图商设置的加密,要转换成百度地图坐标 ...

  7. 地图上绘制任意角度的椭圆_地图上的总椭圆

    地图上绘制任意角度的椭圆 或者,如何选择下班后去海滩的最佳方式 (Or, how to choose the best way to walk to the beach after work) It ...

  8. Matlab运用mapping包在地图上绘制散点图(热力图)

    Matlab运用mapping包在地图上绘制散点图(热力图) 一.前言 Mapping Toolbox是Matlab提供的,一整套包含许多函数跟图形用户界面的工具箱:它可以帮助用户分析几何空间方面的数 ...

  9. python绘制彩色地震剖面_在地图上绘制饼图时“爆炸”楔形图(Python,matplotlib)...

    所以我成功地在地图上绘制了饼图作为标记轴向散射,但我遇到了一些问题,一些楔子"爆炸"出了饼图.我似乎在我的代码中找不到原因,也无法在网上找到解释.这段代码基于示例here,一位同事 ...

最新文章

  1. python映射类型-Python中的映射数据类型 dict
  2. Exchange 2007 容易理解错误的几个地方
  3. 重温强化学习之函数近似
  4. BASIS--Client 锁定和解锁
  5. 安徽理工大学计算机学院蒋群,计算机学院2001级校友十周年聚会
  6. .Net Core 系列:1、环境搭建
  7. 3 PP配置-一般设置-检查计量单位
  8. zabbix 自动发现
  9. hibernate事务详解
  10. php 多线程写入文件,C#_C#实现多线程写入同一个文件的方法,本文实例讲述了C#实现多线程 - phpStudy...
  11. 用python画熊猫_熊猫read_excel()–用Python读取Excel文件
  12. zabbix监控业务进程变动
  13. proxy_cfw全局代理_浏览器代理配置(chromium based(edge)/firefox/IDM)
  14. linux中的段定义的,Linux中的段
  15. Cmd命令检测电脑配置:
  16. 跨端框架 RAX 初体验
  17. vm怎么上传镜像文件到服务器,vmware怎么添加iso镜像文件-vmware添加iso镜像文件的方法 - 河东软件园...
  18. redis的daemonize设置为yes和no有啥区别呀,为啥我两个都试了之后的效果不是像网上说的那样,设置成No的话,redis也会一直运行呀
  19. IM即时通讯源码 聊天交友APP源码
  20. rpm包安装linux系统,包管理 ----- Linux操作系统rpm包安装方式步骤

热门文章

  1. 【docker】docker容器搭建分布式LNMP,附错误及解决方案
  2. 判断素数(质数)高效算法
  3. shell脚本编程学习笔记6(xdl)——字符串截取命令
  4. android移动支付——银联支付
  5. 使用 APlayer 实现音乐播放器
  6. Kafka系统介绍及高性能原理
  7. HC-05 蓝牙模块使用
  8. 软件工程网络15个人阅读作业1(201521123111 陈伟泽)
  9. Xcode 项目运行不成功,有没有朋友可以指导一下
  10. 安超云入选《鲲鹏精选解决方案》