【问题标题】:iPhone - Google map coordinates and double precisioniPhone - 谷歌地图坐标和双精度
【发布时间】:2011-11-04 03:41:29
【问题描述】:

使用 http://maps.google.com/maps/geo?q=.... 返回纬度和经度,如下所示: 48.8616441,2.3448885

在这样的返回字符串中: 200,8,48.8616441,2.3448885

当将 a 解析为返回 CLLocationCoordinate2D 的函数时:

    NSArray* listItems = [locationString componentsSeparatedByString:@","];

    CLLocationDegrees latitude = kInexistantLatitude;
    CLLocationDegrees longitude = kInexistantLongitude;

    if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
        latitude = [[listItems objectAtIndex:2] doubleValue];
        longitude = [[listItems objectAtIndex:3] doubleValue];
    }

    CLLocationCoordinate2D location;
    location.latitude = latitude;
    location.longitude = longitude;

    return location;

它提供给调试器:

纬度 = 48.861644099999999 经度 = 2.3448885000000002

我将许多像这样的项目存储到一个数组中,最后,我解析数组以将它们作为字典项目(标记)写入 plist 文件(我使用一个 NSString appendFormat 给出,如 NSLog):

NSLog(@"%f %f", location.latitude, location.longitude);

48.861644 2.344889

在某个时刻,我将加载该字典,并将读取的内容存储到 CLLocationCoordinate2D 中以将其显示在 MKMapView 上。

所以我有两个问题:

1) 如何在不丢失数字的情况下存储 http 请求给出的返回值并保留我的功能?

2) 如果我能做到这一点,我怎样才能读取这些内容并将其存储到 CLLocationCoordinate2D 中而不会再丢失一个数字?

【问题讨论】:

    标签: iphone string cocoa-touch double coordinates


    【解决方案1】:

    您并没有失去一个数字的精度。当你使用

    NSLog(@"%f %f", location.latitude, location.longitude);
    

    如果您没有指定精度,%f 会根据 ANSI C 标准自动将您的数字截断到小数点后 6 位。

    试试

    NSLog(@"%.8f %.8f", location.latitude, location.longitude);
    

    这应该返回与返回字符串相同的数字。

    【讨论】:

    • 当我有 15 位的精度时,float 肯定会失去它的精度。
    • 我将有最大 8 位精度。此方法允许存储固定的小数位数。如果我有 15 位精度,并且我指定了 %8,它最终会切割最后 7数字。
    【解决方案2】:

    如果您碰巧遇到了原始double 类型的精度问题。

    在计算中,不存在精确的浮点数(在定义的字节数内不能有无限的精度),但它通常具有外观的精度(例如您在调试器输出中看到了)。您通常不必担心这一点,因为精度损失可以忽略不计。

    您可以在Wikipedia entry 中了解更多信息。

    但是,当您 NSLogappendFormat: 时,您可以指定要打印的确切位数。在这种情况下,如果你想要 7 位数字,你会写:

    NSLog(@"%.7f %.7f", location.latitude, location.longitude);
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题。我遇到的条件是,我会从 JSON 中获取 lat long,点击地图后,我必须将 lat long 发送回服务器。服务器以“错误位置”消息响应。分配到 cllocationcoordinate 限制了精度。我通过将来自 JSON 的 lat long 数据作为 NSString 来解决它。创建一个对象类来保存你的 lat 和 long 的 NSString 等价物。“不要将从 JSON 获得的 lat long 存储到 cllocationcoordinate,你可能不得不降低精度。

      NSString * lattodeletetest;
      NSString * longtodeletetest;
      lattodeletetest=[dict objectForKey:@"Lat"];
      longtodeletetest=[dict objectForKey:@"Lon"];
      NSArray *array2=[[NSArray alloc] initWithObjects:lattodeletetest,longtodeletetest, nil];
      NSString * coordinateString2=[array2 componentsJoinedByString:@","];
      safe.strLatLong=coordinateString2;
      NSLog(@"%@",safe.strLatLong); //prints 12.758493847753464,71.488293492438438
      

      通过这种方式,可以保留从 JSON 获得的纬度和经度而无需编辑。

      要将其用作 CLLOcationCoordinate2d ,请使用

       NSString *str=strLatLong;
       NSArray *array=[str componentsSeparatedByString:@","];
       lattodeletetest=[array objectAtIndex:0];
       longtodeletetest=[array objectAtIndex:1];
       CLLocationCoordinate2D location=CLLocationCoordinate2DMake([lattodeletetest doubleValue],[longtodeletetest doubleValue]);
      

      【讨论】:

      • 如果此问题与您链接的问题完全相同(也就是相同的答案解决了两者),那么请将其标记为重复,而不是留下这样的答案。如果这个问题不是重复的,那么请留下完整的答案,而不是仅链接的答案。
      • 是的!添加答案!
      猜你喜欢
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 2015-02-05
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多