OGeek|极客世界-中国程序员成长平台

标题: ios - 使用 GPS 坐标在 iOs map 上创建折线 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:13
标题: ios - 使用 GPS 坐标在 iOs map 上创建折线

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

- (void)locationManagerCLLocationManager *)manager didUpdateToLocationCLLocation *)newLocation fromLocationCLLocation *)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 中收到的坐标,因此我必须使用 prepareForSegue 方法将数据从第一个 ViewController 传递到第二个 ViewController。 我的问题是如何存储所有坐标以将它们放在第二个 ViewController 中以绘制折线?有人帮我吗?

谢谢



Best Answer-推荐答案


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

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

- (void)locationManagerCLLocationManager *)manager didUpdateToLocationCLLocation *)newLocation fromLocationCLLocation *)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]);
    }
}

关于ios - 使用 GPS 坐标在 iOs map 上创建折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18615637/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4