2014-01-26 52 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: