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

实现

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

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

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

-(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);
}

将这个路径添加到地图上

[self.mapView addOverlay:self.routeLine]; 

显示在地图上:

- (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
第四步:启动请求
代码如下:

- (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];
}

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

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

- (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 map
if (nil != self.routeLine) {
[self.mapView addOverlay:self.routeLine];
}
// zoom in on the route.
[self zoomInOnRoute];
}

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

// 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中间,通过iOS开发之Objective-C与JavaScript的交互 这篇文章的方法,用js去绘制轨迹。

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

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

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

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

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

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

  3. 高德地图Web端JavaScript API开发(二)---在地图上绘制(点标注)

    使用高德地图在很多时候需要在地图上标记位置,并且很多时候需要用到自定义的图标去完成这种位置的标记. 当然,这些功能高德地图都为我们准备了,比如常用的地图覆盖物Marker和信息窗体等.这里就先说一下点 ...

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

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

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

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

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

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

  7. android地图画线,绘制折线-在地图上绘制-开发指南-Android 轻量版地图SDK | 高德地图API...

    地图上绘制的线是由 Polyline 类定义实现的,线由一组经纬度(LatLng对象)点连接而成. 绘制一条线 与点标记一样,Polyine的属性操作集中在PolylineOptions类中,添加一条 ...

  8. 【IOS开发高级系列】异步绘制专题

    1 图片处理 1.1 编辑图片的几个方法 第一种 先用UIImage对象加载一张图片 然后转化成CGImageRef放到CGContext中去编辑 第二种 用CGImageCreate函数创建CGIm ...

  9. iOS开发之百度地图的简单集成——标注POI检索

    iOS开发之百度地图的简单集成--标注&POI检索 .h文件 // Created by XK_Recollection on 16/6/15. // Copyright © 2016年 GN ...

最新文章

  1. Nature Cancer | 发现非肿瘤药物的抗癌潜力
  2. Calendar日历小程序
  3. PAT甲级1001 A+B Format:[C++题解]字符串处理
  4. ST17H26尽量避免switch语句
  5. Dubbo调用时报错Invalid token Forbid invoke remote service interface
  6. 带你了解FPGA(5)--Verilog书写规范
  7. 2019 年上万篇论文发表,这 14 篇脱颖而出!
  8. IDEA java 调用 webservice接口
  9. 这45个场景,正在被区块链抽筋扒皮…
  10. android app消息推送,如何进行app消息推送(push)?
  11. 面试经验--Lowe Profero
  12. kms激活代码 win10_Win10_KMS_激活脚本(示例代码)
  13. 单片机C语言DA转换,51单片机PCF8591的DA转换程序详解[含HL-1与HJ-c52 DA代码AD/DA原理图](可直接复......
  14. 埃森哲java笔试题_埃森哲的笔试经验
  15. 【计组之EDA】学了EDA,这些元件符号及常用化简公式你都会了叭(超详细图示ai)
  16. 小花梨的字符串 ——java 美登杯
  17. 【面试】计网知识点复习与总结
  18. python一张纸折叠到珠峰高度_python实现seo疯狂外链发送工具
  19. 年轻人为何一孩都不愿生?中国人口拐点逼近
  20. c语言性格测试小游戏,性格测试小游戏

热门文章

  1. node rimraf模块 递归删除文件夹内容
  2. MySQL基础与navicat使用
  3. ThinkPad E450(c)添加或者更换内存条的一些问题
  4. 通过封装接口拿到淘宝店铺订单,淘宝店铺订单解密接口,淘宝店铺订单明文接口,天猫店铺订单明文接口代码展示
  5. 一键批量替换文本工具
  6. linux内核zfs,Linus Torvalds回应用户抱怨:不建议使用 ZFS On Linux
  7. 阿里云搭建Tomcat+Jdk+Mysql(阿里云系统CentOs)特完整
  8. 进口车在国外到底卖多少钱
  9. 【庖丁解牛】成功解决nginx报错:bind() to 0.0.0.0:8090 failed (13: Permission denied)
  10. OkGo上传文件、图片的用法