我正在尝试用纬度和经度初始化 CLLocation
。然后将其添加到 NSMutableArray
。我 future 的计划是在 map 上显示这些值。但是,我正在为遵循代码而苦苦挣扎,而且我确定我在某个地方犯了一个愚蠢的错误,但由于我是新手而无法弄清楚。如果有人能发现它,我将不胜感激:
这是输出:
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];
最佳答案
正如评论中的人已经说过的那样,在 InfoViewController
中覆盖 -viewDidLoad
,如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
self.trackPointArray = [@[] mutableCopy];
// Other initialization code here
}
关于ios - 为什么不将值添加到 NSMutableArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26101032/