我正在尝试使用纬度和经度初始化CLLocation
。然后将其添加到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];
答案 0 :(得分:3)
正如评论中的人已经说过的那样,在-viewDidLoad
中覆盖InfoViewController
,如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
self.trackPointArray = [@[] mutableCopy];
// Other initialization code here
}