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

实现

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

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

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

[plain] view plaincopyprint?
  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. }
-(void) loadRoute
{
NSString* filePath = [[NSBundle mainBundle] pathForResource:@”route” ofType:@”csv”];
NSString* fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSArray* pointStrings = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];// while we create the route points, we will also be calculating the bounding box of our route
// so we can easily zoom in on it.
MKMapPoint northEastPoint;
MKMapPoint southWestPoint; // create a c array of points.
MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * pointStrings.count);for(int idx = 0; idx < pointStrings.count; idx++)
{
// break the string down even further to latitude and longitude fields.
NSString* currentPointString = [pointStrings objectAtIndex:idx];
NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];CLLocationDegrees latitude = [[latLonArr objectAtIndex:0] doubleValue];
CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue];// create our coordinate and add it to the correct spot in the array
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);MKMapPoint point = MKMapPointForCoordinate(coordinate);//
// adjust the bounding box
//// if it is the first point, just use them, since we have nothing to compare to yet.
if (idx == 0) {
northEastPoint = point;
southWestPoint = point;
}
else
{
if (point.x > northEastPoint.x)
northEastPoint.x = point.x;
if(point.y > northEastPoint.y)
northEastPoint.y = point.y;
if (point.x < southWestPoint.x)
southWestPoint.x = point.x;
if (point.y < southWestPoint.y)
southWestPoint.y = point.y;
}pointArr[idx] = point;}// create the polyline based on the array of points.
self.routeLine = [MKPolyline polylineWithPoints:pointArr count:pointStrings.count];_routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);// clear the memory allocated earlier for the points
free(pointArr);} 

将这个路径添加到地图上

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

显示在地图上:

[plain] view plaincopyprint?
  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. }
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id )overlay
{
MKOverlayView* overlayView = nil;if(overlay == self.routeLine)
{
//if we have not yet created an overlay view for this overlay, create it now.
if(nil == self.routeLineView)
{
self.routeLineView = [[[MKPolylineView alloc] initWithPolyline:self.routeLine] autorelease];
self.routeLineView.fillColor = [UIColor redColor];
self.routeLineView.strokeColor = [UIColor redColor];
self.routeLineView.lineWidth = 3;
}overlayView = self.routeLineView;}return overlayView;} 

效果:

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

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

[plain] view plaincopyprint?
  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. }
- (void)viewDidLoad {[super viewDidLoad];noUpdates = 0;locations = [[NSMutableArray alloc] init];locationMgr = [[CLLocationManager alloc] init];locationMgr.delegate = self;locationMgr.desiredAccuracy =kCLLocationAccuracyBest;locationMgr.distanceFilter  = 1.0f;[locationMgr startUpdatingLocation];}

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

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

[plain] view plaincopyprint?
  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. }
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{noUpdates++;[locations addObject: [NSString stringWithFormat:@"%f,%f",[newLocation coordinate].latitude, [newLocation coordinate].longitude]];[self updateLocation];if (self.routeLine!=nil) {self.routeLine =nil;}if(self.routeLine!=nil)[self.mapView removeOverlay:self.routeLine];self.routeLine =nil;// create the overlay[self loadRoute];// add the overlay to the mapif (nil != self.routeLine) {[self.mapView addOverlay:self.routeLine];}// zoom in on the route. [self zoomInOnRoute];}

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

[plain] view plaincopyprint?
  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. }
// creates the route (MKPolyline) overlay
-(void) loadRoute
{// while we create the route points, we will also be calculating the bounding box of our route// so we can easily zoom in on it. MKMapPoint northEastPoint; MKMapPoint southWestPoint; // create a c array of points. MKMapPoint* pointArr = malloc(sizeof(CLLocationCoordinate2D) * locations.count);for(int idx = 0; idx < locations.count; idx++){// break the string down even further to latitude and longitude fields. NSString* currentPointString = [locations objectAtIndex:idx];NSArray* latLonArr = [currentPointString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];CLLocationDegrees latitude  = [[latLonArr objectAtIndex:0] doubleValue];CLLocationDegrees longitude = [[latLonArr objectAtIndex:1] doubleValue];CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);MKMapPoint point = MKMapPointForCoordinate(coordinate);if (idx == 0) {northEastPoint = point;southWestPoint = point;}else {if (point.x > northEastPoint.x) northEastPoint.x = point.x;if(point.y > northEastPoint.y)northEastPoint.y = point.y;if (point.x < southWestPoint.x) southWestPoint.x = point.x;if (point.y < southWestPoint.y) southWestPoint.y = point.y;}pointArr[idx] = point;}self.routeLine = [MKPolyline polylineWithPoints:pointArr count:locations.count];_routeRect = MKMapRectMake(southWestPoint.x, southWestPoint.y, northEastPoint.x - southWestPoint.x, northEastPoint.y - southWestPoint.y);free(pointArr);}

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

扩展:

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

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

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

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

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

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

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

  3. iOS 在地图上绘制运动轨迹

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

  4. android集成百度地图 驾车路线规划 并在地图上绘制出路线

    1.  设置路线规划监听 mSearch.setOnGetRoutePlanResultListener(getRoutePlanListener);//设置路线规划监听 2.初始化路线监听器 /*路 ...

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

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

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

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

  7. 地图上分成一块一块区域 高德地图_在谷歌地图上绘制行政区域轮廓【结合高德地图的API】...

    实现思路: 1.利用高德地图行政区域API获得坐标列表 2.将坐标列表绘制在谷歌地图上[因为高德地图和国内的谷歌地图都是采用GCJ02坐标系,所有误差很小,可以不进行坐标误差转换] 注意点: 1.用百 ...

  8. plotly系列| 使用plotly在地图上绘制散点图和密度图

    目录 Plotly简介 在地图上制作散点图和密度图 1 . 在python中使用pip命令安装 2 .导入包 3 .读取文件 4 . 在地图上绘制散点图 5 . 绘制密度图,其属性与绘制散点图相似 关 ...

  9. 在Arcgis地图上绘制Echarts热力图(Heatmap)

    在Arcgis地图上绘制Echarts热力图(Heatmap) 2018-08-30 10:18 [原创]本文地址:https://www.cnblogs.com/qiaoge0923/p/95623 ...

最新文章

  1. LLVM与Clang局部架构与语法分析
  2. java实现线程同步的方法_Java实现线程同步方法及原理详解
  3. 二十年后我发明了保姆机器人作文_我想发明保姆机器人作文700字
  4. Linux下创建软链接
  5. jAVA not find main_java编程时出现Could not find the main class 怎么解决???
  6. 互联网晚报 | 9月22日 星期三 | 中国电信控股股东拟40亿元增持;碧桂园服务100亿收购富良环球;搜狗浏览器论坛即将下线...
  7. 消除拖延的方法_如何消灭拖延症,并彻底消除想法
  8. 力扣 数组中的最长山脉
  9. 【资料整理】Eclipse快捷键
  10. Pascal数据结构与算法
  11. 云计算(Cloud Computing)
  12. 域名讲解(一)域名基础概念
  13. 综合练习--The Eighth Week
  14. STM32中的IDR和ODR寄存器
  15. 为了旅游和梁定郊大吵一次,此行贿赠喜爱的朋友!!!
  16. oracle 重建控制文件 恢复数据库
  17. 在电脑上怎么做判断题打√或x_苹果Mac电脑知识竞赛考试题
  18. jquery ajax 分页 java_jquery +ajax 分页实现
  19. 计算机丢失ch365dll,修复ch365dll.dll
  20. Jpa使用详解(Hibernate)

热门文章

  1. IDEA2021 event log提示 打开/关闭
  2. Happy Wu-新人报道!
  3. Go语言读取文件的常用方式
  4. 如何用一根网线实现在两台电脑之间传输数据
  5. JDK原生网络编程-NIO基础入门
  6. JAVA:实现BloomFilter二进制向量数据结构算法(附完整源码)
  7. PC实现Win10/原生安卓双系统
  8. python一些常用函数
  9. 教育知识与能力(1)
  10. 苟日新,日日新,又日新