简体   繁体   English

如何在iOS 6中将步行轨迹或路线显示为地图叠加层

[英]How to display a walking track or route as a Map Overlay in iOS 6

I've been trying to search the internet for a tutorial with some sample code that shows how to display a track or route as an overlay on a map. 我一直在尝试在互联网上搜索包含一些示例代码的教程,该示例代码显示了如何在地图上以叠加层的形式显示轨迹或路线。 I've found some tutorials but they are for displaying circles and other shapes. 我找到了一些教程,但是它们是用于显示圆和其他形状的。 I was wanting to display a point by point track. 我想显示一个逐点的轨迹。

Could anyone give me a link to a good tutorial or could someone point me in the right direction? 谁能给我链接到一个好的教程,或者有人可以向我指出正确的方向?

Any help would be greatly appreciated. 任何帮助将不胜感激。

Cheers 干杯

推荐的方式显示在Apples Breadcrumbs演示应用程序中。

Try using MKPolyline. 尝试使用MKPolyline。 You make an array of CLLocation with the coords of each turn point along your route. 您将沿路线的每个转折点的坐标组成一个CLLocation数组。 You initialize the MKPolyline object with that array. 您使用该数组初始化MKPolyline对象。 This will draw a solid line between each point you specified. 这将在您指定的每个点之间画一条实线。 You can then adjust the Line's dashPattern and dashPhase if your points are far enough apart that you need to break up the line. 然后,如果您的点之间的距离足够远以至于需要断开线,则可以调整线的dashPattern和dashPhase。

I started with an NSMutableArray of the lat,long points called points: 我从一个称为点的经点的NSMutableArray开始:

NSMutableArray *currentPoint;
int count = [points count];
double lat, long;

CLLocationCoordinate2D unitPath[count];
for (NSInteger index = 0; index < count; index++)
{
    currentPoint = [points objectAtIndex:index];

    Lat = [[currentPoint objectAtIndex:ORDER_TARGLAT] doubleValue];
    Long = [[currentPoint objectAtIndex:ORDER_TARGLONG] doubleValue];
    CLLocation *pathPoint = [[CLLocation alloc] initWithLatitude:Lat longitude:Long];
    unitPath[index] = pathPoint.coordinate;
}

MKPolyline *routeLine = [MKPolyline polylineWithCoordinates:unitPath count:count];

routeLine.title = _title;
routeLine.subtitle = [[NSString alloc] initWithFormat:@"%d",ID];
routeLine.strokeColor = [UIColor whiteColor];
routeLine.fillColor = [UIColor whiteColor];
NSNumber *lineGapSize = [NSNumber numberWithInteger:10];
routeLine.dashPattern = [NSArray arrayWithObjects:lineGapSize,lineGapSize,nil];
routeLine.dashPhase = lineDashPhase;
routeLine.lineWidth = 4; 

[mapView addOverlay:routeLine]

PS ORDER_TARGLAT and ORDER_TARGLONG were just part of an enum specifying the position of items in my array. PS ORDER_TARGLAT和ORDER_TARGLONG只是一个枚举的一部分,该枚举指定了数组中各项的位置。 Replace as appropriate for your code. 根据您的代码进行替换。

MapView* mapView = [[[MapView alloc] initWithFrame:
                         CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)] autorelease];

[self.view addSubview:mapView];

Place* home = [[[Place alloc] init] autorelease];
home.name = @"Home";
home.description = @"Sweet home";
home.latitude = 29.860323999999990000;
home.longitude =77.893305000000050000;

Place* office = [[[Place alloc] init] autorelease];
office.name = @"Office";
office.description = @"Bad office";
office.latitude = 28.644251400000000000;
office.longitude = 77.121190500000010000;

[mapView showRouteFrom:home to:office];

use this one in ios 7.0 在ios 7.0中使用这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM