2014-01-26 47 views
0

嗨! 我是Objective-C的新手。 我正在开发一个跟踪器应用程序,其中我必须存储所有gps坐标,然后分析它们以跟踪车辆的路径。我使用两个数组来存储纬度和经度点。当以后尝试使用Latitude点进行计算时,它给了我一个错误,指出“在索引1处出现绑定异常”,但数组的大小向我显示为4.下面是我的ViewController的代码片段。 h,请看一下,让我知道我是如何做到这一点的。Reg位置管理员:didUpdateLocation

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

} 
+0

请注意,'sizeof'函数返回给定变量使用的内存的长度。结果4是arrayWithLat指针的大小(不是数组中有多少个对象)。要获取NSArray中的对象的数量,请改用其“count”方法。 – Anna

回答

0

我觉得你的问题是在这里:

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#>) 
} 

在阵列的第一指标开始从0到n-1,所以这里i <= sizeof(arraywithlat)错,i < sizeof(arraywithlat)应该的。 和NSNumber *tla2 = [arraywithlat objectAtIndex:i+1]是错误的。改变它,因为我会等于n-1,那么你会得到超出界限的例外。你能解释一下这个循环是什么,可能是我们帮你改写它

另外请注意- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation代理方法已被弃用。改为使用locationManager:didUpdateLocations: