Question

Hi !! I am new to Objective-C. I am developing a tracker application, in which I have to store all the gps coordinates and then analyse them to track the path in which the vehicle was. I am using two arrays for storing the latitude and longitude points. When trying to use the Latitude points for calculations later, its giving me an error saying "Out of bound exception at index 1", but the size of the array is being shown to me as 4. Here is the code snippet of my ViewController.h, please take a look and let me know how I do this.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    [trackingMapView addPoint:newLocation];
    float time = -[startDate timeIntervalSinceNow];
    displayLabel.text = [NSString stringWithFormat:@"Speed: %.02f mph", distance * 2.2369 / time];
    // if there was a previous location
    if (oldLocation != nil)
    {
        // add distance from the old location tp the total distance
        distance += [newLocation getDistanceFrom:oldLocation];
    }
    // create a region centered around the new point
    MKCoordinateSpan span = MKCoordinateSpanMake(0.005, 0.005);

    // create a new MKCoordinateRegion centered around the new location
    MKCoordinateRegion region = MKCoordinateRegionMake(newLocation.coordinate, span);

    //New Added Code
    CLLocationSpeed speed = newLocation.speed ;
    speed = speed * 2.2369 ;
   /* if (speed > 10.000)
        {
            NSString *message = @"Slow Down !!" ;
            UIAlertView *alertforspeed = [[UIAlertView alloc] initWithTitle:@"Speed Monitor" message: message delegate:self cancelButtonTitle:@"Return" otherButtonTitles:nil];
            [alertforspeed show];
            [alertforspeed release];
        }*/
     //NEW CODE 2
     //----------------------------------------------
    arraywithlat= [[NSMutableArray alloc]init];
    arraywithlong= [[NSMutableArray alloc]init];
    NSNumber *lati = [NSNumber numberWithFloat:newLocation.coordinate.latitude];
    NSNumber *longi = [NSNumber numberWithFloat:newLocation.coordinate.longitude];
    [arraywithlat addObject:lati];
    [arraywithlong addObject:longi];
    //[locations addObject:newLocation];
    NSLog(@"Array with lat : %@" , arraywithlat);
    NSLog(@"Array with long: %@", arraywithlong);
        //-----------------------------------------------
     int i;
     float r1 = 0.0, r2, result ;
     NSLog(@"IN LANE TRACKING, BEFORE IFF");
     int exsize = sizeof(arraywithlat);
     NSLog(@"SIze of array before the loop: %d", exsize);
     if (sizeof(arraywithlat) >= 4) {
     NSLog(@"CROSSED THE IFFFFF");
     NSLog(@"SIXE OF ARRAYWITHLAT: %lu" , sizeof(arraywithlat));
     for (i = 0; i <= sizeof(arraywithlat); i++) {
     NSNumber *tla1 = [arraywithlat objectAtIndex:i];
     float temp1 = [tla1 floatValue];
     NSLog(@"temp111111111: %f",temp1);
     NSNumber *tla2 = [arraywithlat objectAtIndex:i+1] ;
     float temp2 = [tla2 floatValue];
     float r1 = temp1 - temp2 ;
     // float r1 = [NSNumber numberWithFloat:tla2] - [NSNumber numberWithFloat:tla1];
     //float r1 = [tla2 floatValue] - [tla1 floatValue];
     r1 = r1 * r1 ;
     //float tla = arraywithlat objectAtIndex: i+1) - (arraywithlat objectAtIndex: i);
     //float tlo = [arraywithlong obj]
     // float tr1 = powf(((arraywithlat objectAtIndex:(i+1))-([arraywithlat objectatIndex:(i)])), <#float#>)
     }
     }
     NSLog(@"r1 after cal: %f",r1);

    // reposition the map t show the new point
    [mapView setRegion:region animated:YES];

}
Was it helpful?

Solution

I think your problem is here:

for (i = 0; i <= sizeof(arraywithlat); i++) {
     NSNumber *tla1 = [arraywithlat objectAtIndex:i];
     float temp1 = [tla1 floatValue];
     NSLog(@"temp111111111: %f",temp1);
     NSNumber *tla2 = [arraywithlat objectAtIndex:i+1] ;
     float temp2 = [tla2 floatValue];
     float r1 = temp1 - temp2 ;
     // float r1 = [NSNumber numberWithFloat:tla2] - [NSNumber numberWithFloat:tla1];
     //float r1 = [tla2 floatValue] - [tla1 floatValue];
     r1 = r1 * r1 ;
     //float tla = arraywithlat objectAtIndex: i+1) - (arraywithlat objectAtIndex: i);
     //float tlo = [arraywithlong obj]
     // float tr1 = powf(((arraywithlat objectAtIndex:(i+1))-([arraywithlat objectatIndex:(i)])), <#float#>)
}

first indexes in array starts from 0 to n-1, so i <= sizeof(arraywithlat) here wrong, i < sizeof(arraywithlat) should be. And NSNumber *tla2 = [arraywithlat objectAtIndex:i+1] is wrong. change it, coz when i will be equal to n-1 then you get exception out of bound. And can you explain what does this loop, may be we help you to rewrite it

Also note that - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation method of delegate is deprecated. Use locationManager:didUpdateLocations: instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top