2014-09-29 58 views
0

我想用经度和纬度初始CLLocation。然后将其添加到NSMutableArray。我未来的计划是在地图上显示这些值。然而,我正在努力处理下面的代码,我相信我正在做一个愚蠢的错误,但无法弄清楚,因为我是新手。我很感激,如果任何人都可以发现它:为什么值不被添加到NSMutableArray?

这里是输出:

2014-09-29 23:16:02.261 JourneyTracker[8149:343967] lat= 37.33446146 long= -122.04380955 
2014-09-29 23:16:02.261 JourneyTracker[8149:343967] before numberOfSteps== 0 
2014-09-29 23:16:02.462 JourneyTracker[8149:343967] After numberOfSteps== 0 
2014-09-29 23:16:02.462 JourneyTracker[8149:343967] lat= 37.33445363 long= -122.04302852 
2014-09-29 23:16:02.462 JourneyTracker[8149:343967] before numberOfSteps== 0 
2014-09-29 23:16:02.680 JourneyTracker[8149:343967] After numberOfSteps== 0 
2014-09-29 23:16:02.680 JourneyTracker[8149:343967] lat= 37.33445283 long= -122.04113765 
2014-09-29 23:16:02.680 JourneyTracker[8149:343967] before numberOfSteps== 0 
2014-09-29 23:16:02.945 JourneyTracker[8149:343967] After numberOfSteps== 0 
2014-09-29 23:16:02.945 JourneyTracker[8149:343967] lat= 37.33445945 long= -122.04342255 
2014-09-29 23:16:02.946 JourneyTracker[8149:343967] before numberOfSteps== 0 
2014-09-29 23:16:03.053 JourneyTracker[8149:343967] After numberOfSteps== 0 
2014-09-29 23:16:03.054 JourneyTracker[8149:343967] 111numberOfSteps== 0 
2014-09-29 23:16:09.390 JourneyTracker[8149:343967] 2222numberOfSteps== 0 
2014-09-29 23:16:10.238 JourneyTracker[8149:343967] 3333numberOfSteps== 0 

正如你可以看到它去四次循环中,但没有将被添加:

for(Coordinates *cord in coordFromDB) 
    { 
     NSLog(@"lat= %@ long= %@ ",cord.lat,cord.longt); 

     CLLocationDegrees Latitude = [cord.lat doubleValue]; 
     CLLocationDegrees Longitude = [cord.longt doubleValue]; 

     lastlat = [cord.lat doubleValue]; 
     lastlong = [cord.longt doubleValue]; 

     CLLocationCoordinate2D locationCoordinates = CLLocationCoordinate2DMake(Latitude, Longitude); 

     //Zoom map to show current location 
     MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(locationCoordinates, 2000, 2000); 
     MKCoordinateRegion adjustRegion = [self.mapView regionThatFits:viewRegion]; 
     [self.mapView setRegion:adjustRegion animated:YES]; 

     CLLocation *currLocation = [[CLLocation alloc] initWithLatitude:lastlong longitude:lastlong]; 

     NSLog(@"before numberOfSteps== %d",trackPointArray.count); 
     //Store latest location in stored track array 

     [trackPointArray addObject:currLocation]; 

     NSLog(@"After numberOfSteps== %d",[trackPointArray count]); 
    } 


    NSLog(@"111numberOfSteps== %d",trackPointArray.count); 


    NSInteger numberOfSteps =trackPointArray.count; 


    NSLog(@"2222numberOfSteps== %d",numberOfSteps); 


    CLLocationCoordinate2D coordinates[numberOfSteps]; 

    for(NSInteger index=0;index<numberOfSteps;index++) 
    { 
     CLLocation *location = [trackPointArray objectAtIndex:index]; 
     CLLocationCoordinate2D coordinate2 = location.coordinate; 
     coordinates[index] = coordinate2; 
    } 


    routeLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps]; 
    [self.mapView addOverlay:routeLine]; 
    NSLog(@"3333numberOfSteps== %d",numberOfSteps); 

} 

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay 
{ 
    if (![overlay isKindOfClass:[MKPolygon class]]) { 
     return nil; 
    } 
    MKPolygon *polygon = (MKPolygon *)overlay; 
    MKPolygonRenderer *renderer = [[MKPolygonRenderer alloc] initWithPolygon:polygon]; 
    renderer.fillColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.4]; 
    return renderer; 
} 

这里是定义变量:

@implementation InfoViewController 

NSMutableArray *trackPointArray; 
MKMapRect routeRect; 
MKPolylineView* routeLineView; 
MKPolyline* routeLine; 

double lastlat; 
double lastlong; 

.... 

更新

我已经加入这个和它的工作:

- (void)viewDidLoad { 

    [super viewDidLoad]; 
    trackPointArray = [NSMutableArray new]; 
+2

你有你的初始化可变数组?在viewdidload trackPointArray = [NSMutableArray new]中添加此代码; – 2014-09-29 13:27:45

+2

可能你的'NSMutableArray'是未初始化的。 – Desdenova 2014-09-29 13:27:59

+2

你有没有初始化'trackPointArray'指向一个实际的数组对象? – 2014-09-29 13:28:16

回答

3

随着人们的意见已经说了,在InfoViewController像这样覆盖-viewDidLoad

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.trackPointArray = [@[] mutableCopy]; 
    // Other initialization code here 
} 
相关问题