将你的运行轨迹绘制在地图上面

作者:求生之路2地图

 将你的运行轨迹绘制在地图上面。 在地形图上绘制出您运维的轨迹 

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;



} 

看下从文件中读取数据绘制的轨迹路线效果:

图片 1

接下来大家在从文件中读取地方的章程改成从用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去绘制轨迹。

总结:那篇作品大家介绍了生龙活虎种平淡无奇的才具完毕:在地图上制图出你运营的轨迹。

来自

本文由美高梅游戏官网娱乐发布,转载请注明来源

关键词: