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

简介:

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

}
复制代码

将这个路径MKPolyline对象添加到地图上

[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去绘制轨迹。

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








本文转自麒麟博客园博客,原文链接:http://www.cnblogs.com/zhuqil/archive/2011/08/09/2131708.html,如需转载请自行联系原作者
相关文章
|
1月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
85 3
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
39 0
|
6天前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
【4月更文挑战第18天】**Cocoa Touch** 是iOS和Mac OS X应用的核心框架,包含面向对象库、运行时系统和触摸优化工具。它提供Mac验证的开发模式,强调触控接口和性能,涵盖3D图形、音频、网络及设备访问API,如相机和GPS。是构建高效iOS应用的基础,对开发者至关重要。
9 0
|
21天前
|
开发工具 Swift iOS开发
利用SwiftUI构建动态用户界面:iOS开发新范式
【4月更文挑战第3天】 随着苹果不断推进其软件开发工具的边界,SwiftUI作为一种新兴的编程框架,已经逐渐成为iOS开发者的新宠。不同于传统的UIKit,SwiftUI通过声明式语法和强大的功能组合,为创建动态且响应式的用户界面提供了一种更加简洁高效的方式。本文将深入探讨如何利用SwiftUI技术构建具有高度自定义能力和响应性的用户界面,并展示其在现代iOS应用开发中的优势和潜力。
|
2月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
196 2
|
3月前
|
数据可视化 iOS开发
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
40 4
|
3月前
|
iOS开发
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
28 1
|
3月前
|
存储 安全 数据安全/隐私保护
IOS开发数据存储:解释一下 iOS 中的 Keychain,它的作用是什么?
IOS开发数据存储:解释一下 iOS 中的 Keychain,它的作用是什么?
90 4
|
3月前
|
存储 数据库 iOS开发
IOS开发数据存储:什么是 CoreData?如何在应用中使用它?
IOS开发数据存储:什么是 CoreData?如何在应用中使用它?
36 0