我正在尝试使用文件中的路线绘制地图。我正在尝试通过关注此博客(http://spitzkoff.com/craig/?p=108)来学习此过程。
当进程命中我的函数时,它会终止(我在下面给出了错误消息)
- (void) drawRoute {
//
// load the points from our local resource
//
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"route" ofType:@"csv"];
NSString* fileContents = [NSString stringWithContentsOfFile:filePath];
NSArray* pointStrings = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSMutableArray* points = [[NSMutableArray alloc] initWithCapacity: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];
CLLocation* currentLocation = [[[CLLocation alloc] initWithLatitude:latitude longitude:longitude] autorelease];
[points addObject:currentLocation];
}
// CREATE THE ANNOTATIONS AND ADD THEM TO THE MAP
// first create the route annotation, so it does not draw on top of the other annotations.
CSRouteAnnotation* routeAnnotation = [[[CSRouteAnnotation alloc] initWithPoints:points] autorelease];
[secondMap addAnnotation:routeAnnotation];
// create the rest of the annotations
CSMapAnnotation* annotation = nil;
// create the start annotation and add it to the array
annotation = [[[CSMapAnnotation alloc] initWithCoordinate:[[points objectAtIndex:0] coordinate]
annotationType:CSMapAnnotationTypeStart
title:@"Start Point"] autorelease];
[secondMap addAnnotation:annotation];
// create the end annotation and add it to the array
annotation = [[[CSMapAnnotation alloc] initWithCoordinate:[[points objectAtIndex:points.count - 1] coordinate]
annotationType:CSMapAnnotationTypeEnd
title:@"End Point"] autorelease];
[secondMap addAnnotation:annotation];
[points release];
// center and size the map view on the region computed by our route annotation.
[secondMap setRegion:routeAnnotation.region];
}
错误讯息:
[Session started at 2010-07-30 01:22:13 -0400.]
2010-07-30 01:22:19.078 ActualTry[4893:20b] Found center of new Route Annotation at 0.000000, 0
2010-07-30 01:22:19.079 ActualTry[4893:20b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
2010-07-30 01:22:19.080 ActualTry[4893:20b] Stack: (
807902715,
2492862011,
807986683,
807986522,
810976489,
810572359,
13379,
12278,
10121,
814709201,
815110321,
815119058,
815114270,
814813151,
814722763,
814748641,
839148405,
807687520,
807683624,
839142449,
839142646,
814752238,
9380,
9234
)
如果有人可以帮助我,我将非常感激。我知道这是基本的gdb调试,但我无法解决这个问题。
谢谢。
答案 0 :(得分:0)
错误消息表明您正在尝试访问阵列中不存在的对象。看看你的代码,似乎pointStrings数组是空的。你确定你的csv文件有数据吗?尝试使用NSlog打印pointStrings。