• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 使用 GPS 坐标在 iOs map 上创建折线

[复制链接]
菜鸟教程小白 发表于 2022-12-12 14:13:45 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在我的应用程序中,我计算了脚步声,现在我想知道速度,我想存储 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap