简体   繁体   English

将值数组放入CLLocationCoordinate2D的绝佳方法

[英]Elegant Way to Put Array of Values Into CLLocationCoordinate2D

Is there an elegant way to create a CLLocationCoordinate2D object from an array of NSDecimalNumber values? 有没有一种优雅的方法可以从NSDecimalNumber值数组创建CLLocationCoordinate2D对象? Perhaps a recursive function? 也许是递归函数?

I'm new to objective C and am having difficulty with this. 我是目标C的新手,对此感到困难。

Below is an example of the data structures I'm working with. 以下是我正在使用的数据结构的示例。


an array of NSDecimalNumber values: NSDecimalNumber值的数组:

NSLog(@"path: %@", [path description]);

returns 退货

path: (
    "-71.4826609644423",
    "27.7231737704478",
    "-71.4826608497223",
    "27.7231120144099",
    "-71.4826391681679",
    "27.7228678610506",
    ...
    "-71.482414263977",
    "27.7225217655116",
)

I can manually create the CLLocationCoordinate2D object like this: 我可以像这样手动创建CLLocationCoordinate2D对象:

CLLocationCoordinate2D pathCoords[5]={
    CLLocationCoordinate2DMake(27.7231737704478,-71.4826609644423),
    CLLocationCoordinate2DMake(27.7231120144099,-71.4826608497223),
    CLLocationCoordinate2DMake(27.7228678610506,-71.4826391681679),
    ...
    CLLocationCoordinate2DMake(27.7225217655116,-71.482414263977),
};

Any coding suggestions are welcome. 欢迎任何编码建议。 Thanks! 谢谢!

I'm assuming you have already created a CLLocationCoordinate2D structure called pathCoords of size 5. 我假设您已经创建了一个名为5的pathCoordsCLLocationCoordinate2D结构。

for(int i=0;i<[path count]/2;i++)
   pathCoords[i] = CLLocationCoordinate2DMake([path objectAtIndex:2*i+1], [path objectAtIndex:2*i]);

The nature of your problem is not recursive, so if you go recursive - it's an overkill. 问题的本质不是递归的,所以如果您递归的话,那就太过分了。

The final solution including typecasting the NSDecimalNumbers to doubleValues: 最终解决方案包括将NSDecimalNumbers类型转换为doubleValues:

for(int i=0; i<[path count]/2; i++) {
        pathCoords[i] = CLLocationCoordinate2DMake([[path objectAtIndex:2*i+1] doubleValue], [[path objectAtIndex:2*i] doubleValue]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM