views:

72

answers:

1

I am trying to draw a map with routes from a file. I am trying to learn this procedure by following this blog (http://spitzkoff.com/craig/?p=108).

When the process hits my function, it terminates (I have given the error message below)

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

}

Error message:

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

I will be really thankful if someone could help me in this. I know that this is basic gdb debugging, but I am not able to figure this out.

Thank you.

A: 

The error message says that you are trying to access an object in the array which doesn't exist. Looking at your code, it seems pointStrings array is empty. Are you sure your csv file has data? Try printing the pointStrings with NSlog.

lukya
Thank you. I had added the file physically in the directory but not under "resources" in the XCode project.
Nithin