2

在我的应用程序中,我计算了脚步声,现在我想知道速度,我想存储 GPS 坐标以在另一个 ViewController 中绘制一条折线。我认为我可以使用以下代码存储此坐标:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        locationArray = [[NSMutableArray alloc]init];
        [locationArray addObject:currentLocation];
        speed = (int) currentLocation.speed * 3.6;
        self.labelSpeed.text = [NSString stringWithFormat:@"%d Km/h",speed];
        NSLog(@"%@", [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
        NSLog(@"%@", [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
    }
}

但它不起作用,因为locationArray它只存储了 GPS 传感器接收到的最后一个坐标。为了更好地向您解释我正在尝试开发的内容,我将在这里写一些更具体的内容:在第一个 ViewController 中,我想显示 2 个标签,我在其中计算步数和速度。所以在这个 ViewController 中我必须接收坐标数据,我认为这个数据应该插入到NSMutableArray. 在第二个 ViewController 中,我显示了一个标签,我将在其中插入总步数(通过使用prepareForSegue方法),在 MapView 下方,我将在其中绘制一条折线以显示我制作的路径。为此,我需要在第一个 ViewController 中收到的坐标,所以我必须通过使用将数据从第一个 ViewController 传递到第二个 ViewControllerprepareForSegue方法。我的问题是如何存储所有坐标以将它们放在第二个 ViewController 中以绘制折线?有人帮我吗?

谢谢

4

1 回答 1

1

您只存储最后一个坐标,因为每次获得新位置时都会初始化数组,将分配线移动到另一个方法(如viewDidLoad)并擦除didUpdateToLocation.

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationArray = [[NSMutableArray alloc]init];
    //.... more code
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        //locationArray = [[NSMutableArray alloc]init]; //This line doesn't go here
        [locationArray addObject:currentLocation];
        speed = (int) currentLocation.speed * 3.6;
        self.labelSpeed.text = [NSString stringWithFormat:@"%d Km/h",speed];
        NSLog(@"%@", [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
        NSLog(@"%@", [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);
    }
}
于 2013-09-04T14:23:53.213 回答