【问题标题】:CLLocationCoordinate2D without knowing how many will be in the array?CLLocationCoordinate2D 不知道数组中有多少?
【发布时间】:2010-08-12 21:08:21
【问题描述】:

我需要使用类似下面的东西来构建一个数组:

CLLocationCoordinate2D  points[4];


    points[0] = CLLocationCoordinate2DMake(41.000512, -109.050116);

    points[1] = CLLocationCoordinate2DMake(41.002371, -102.052066);

    points[2] = CLLocationCoordinate2DMake(36.993076, -102.041981);

    points[3] = CLLocationCoordinate2DMake(36.99892, -109.045267);

问题是,我永远不会知道数组中有多少项,所以我可以指定计数。有没有办法创建一个 CLLocationCoordinate2D 数组,然后在不知道最终总数的情况下插入新坐标?

编辑:我的最终目标是使用需要 CLLocationCoordinate2D 数组的 polylineWithCoordinates 方法使用坐标制作 MKPolyline。

【问题讨论】:

    标签: iphone objective-c mkmapview cllocation


    【解决方案1】:
    // unpacking an array of NSValues into memory
    CLLocationCoordinate2D *points = malloc([mutablePoints count] * sizeof(CLLocationCoordinate2D));
    for(int i = 0; i < [mutablePoints count]; i++) {
        [[mutablePoints objectAtIndex:i] getValue:(points + i)];
    }
    
    MKPolyline *myPolyline = [MKPolyline polylineWithCoordinates:points count:[mutablePoints count]];
    free(points);
    

    【讨论】:

    • 不要忘记在那个 .m 文件中添加 #include 并且,函数是 sizeof(...) 而不是 size_of(...) ...刚刚救了你15分钟。 :D 否则效果很好!
    • 这里实际发生了什么:CLLocationCoordinate2D *points = malloc([mutablePoints count] * sizeof(CLLocationCoordinate2D));如果有人能指出这一点,那就太好了。我们不是在创建一个 C 数组,不是吗?
    • 我们确实在创建一个 C 数组。请参阅 polylineWithCoordinates:count: 的文档。
    • 什么是可变点?
    • @igrrik 它只是一个 NSArray。我正在跟进 Dave DeLong 的回答。
    【解决方案2】:

    将它们放入NSValue 对象中,然后将它们放入NSMutableArray 中。

    【讨论】:

    • 但我正在尝试使用 polylineWithCoordinates 方法从中创建一个 MKPolyline。
    • 你必须将它们从你的 NSMutableArray 解压到内存中,然后用一些指针数学运算。
    猜你喜欢
    • 2020-02-11
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 2019-05-12
    相关资源
    最近更新 更多