2010-07-30 61 views
0

我试图从文件中绘制路线图。我正在尝试通过关注此博客来了解此过程(http://spitzkoff.com/craig/?p=108)。新手iphone开发问题

当进程打我的功能,它终止(我已经给下面的错误消息)

- (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

错误消息说您正试图访问数组中不存在的对象。看看你的代码,看起来pointStrings数组是空的。你确定你的CSV文件有数据吗?尝试用NSlog打印pointStrings。

+0

谢谢。我已将该文件物理地添加到目录中,但不在XCode项目的“资源”下。 – Nithin 2010-07-30 12:50:01