在iOS Core Data中存储高精度纬度/经度数

时间:2022-04-10 16:05:59

I'm trying to store Latitude/Longitudes in core data. These end up being anywhere from 6-20 digit precision.

我正在尝试将纬度/经度存储在核心数据中。这些最终可达到6-20位精度。

And for whatever reason, i had them as floats in Core Data, its rounding them and not giving me the exact values back. I tried "decimal" type, with no luck either.

无论出于何种原因,我把它们作为核心数据中的浮点数,将它们四舍五入,并没有给我准确的值。我试过“十进制”类型,也没有运气。

Are NSStrings my only other option?

NSStrings是我唯一的选择吗?

EDIT

NSManagedObject:

@interface Event :  NSManagedObject  
{
}

@property (nonatomic, retain) NSDecimalNumber * dec;
@property (nonatomic, retain) NSDate * timeStamp;
@property (nonatomic, retain) NSNumber * flo;
@property (nonatomic, retain) NSNumber * doub;

Here's the code for a sample number that I store into core data:

这是我存储到核心数据中的样本编号的代码:

NSNumber *n = [NSDecimalNumber decimalNumberWithString:@"-97.12345678901234567890123456789"];

The above value printed. Sweet, value I expected:

以上值打印。甜蜜,我期待的价值:

Printing description of n:

-97.12345678901234567890123456789

Code to access it again:

再次访问它的代码:

NSNumber *n = [managedObject valueForKey:@"dec"];
NSNumber *f = [managedObject valueForKey:@"flo"];
NSNumber *d = [managedObject valueForKey:@"doub"];

Printed values:

Printing description of n:
    -97.1234567890124

    Printing description of f:
    <CFNumber 0x603f250 [0xfef3e0]>{value = -97.12345678901235146441, type = kCFNumberFloat64Type}

    Printing description of d:
    <CFNumber 0x6040310 [0xfef3e0]>{value = -97.12345678901235146441, type = kCFNumberFloat64Type}

7 个解决方案

#1


10  

Have you used the NSNumber wrapper?

你使用过NSNumber包装器吗?

Configure your store to use NSNumber, instead of float or decimal, and use this to save the coordinates:

将商店配置为使用NSNumber而不是float或decimal,并使用它来保存坐标:

[NSNumber numberWithDouble:coordinate.latitude]
//Same for longitude.

#2


1  

Try using the Double data type in Core Data. Since your location coordinates are doubles, makes sense to use the same in Core Data. Though that may be available just now (iOS 5).

尝试在Core Data中使用Double数据类型。由于您的位置坐标是双精度数,因此在核心数据中使用相同的位置是有意义的。虽然现在可以使用(iOS 5)。

#3


1  

When Core Data stores data in SQLite, it uses numeric columns. SQLite stores numbers as--at most--8-byte values, whether integer or floating-point. So, while an NSDecimalNumber would be quite happy to accurately represent these coordinate values, round-tripping them through a Core Data decimal attribute backed by SQLite will munge them.

当Core Data在SQLite中存储数据时,它使用数字列。 SQLite将数字存储为 - 最多 - 8字节值,无论是整数还是浮点数。因此,虽然NSDecimalNumber非常乐意准确地表示这些坐标值,但是通过SQLite支持的核心数据十进制属性对它们进行四舍五入将使它们变得非常好。

#4


0  

since coordinate ranges are well-defined, you can add a few digits of accuracy (compared to double) by using a 64 bit int representation (or even multiple).

由于坐标范围是明确定义的,因此可以使用64位int表示(甚至多次)来添加几位精度(与double相比)。

in reality, your sources and targets may not make use of or provide this much accuracy so... there may not be much to gain.

实际上,您的来源和目标可能无法使用或提供如此高的准确性,因此......可能没有太大的收获。

#5


0  

I believe the truncation problem could be on the logging, not on the actual data stored in Core Data.

我认为截断问题可能是日志记录,而不是存储在Core Data中的实际数据。

Can you confirm by posting the code you are using to log the output?

您可以通过发布用于记录输出的代码来确认吗?

I say this because I've noticed that when logging some of my longer NSString fields, Core Data description will only display ~50 characters or so, which might lead you to think it is truncating the data, but it's actually just truncating the description of it.

我这样说是因为我注意到在记录我的一些较长的NSString字段时,Core Data描述只会显示约50个字符左右,这可能会让你认为它正在截断数据,但它实际上只是截断了它。

#6


0  

In your data model editor, make sure the types for your lat/long fields are set to double. Otherwise CoreData will do the conversion for you, and the result will be not what you expect (in this case, the decimals will get dropped).

在数据模型编辑器中,确保lat / long字段的类型设置为double。否则CoreData将为您进行转换,结果将不是您所期望的(在这种情况下,小数将被删除)。

#7


0  

strongly recommend use NSDecialNumber for storing if you need precise value or compare in your code. you might have different value after store and get value back from core data in double fields.

如果您需要精确值或在代码中进行比较,强烈建议使用NSDecialNumber进行存储。存储后可能会有不同的值,并从双字段中的核心数据中获取值。

#1


10  

Have you used the NSNumber wrapper?

你使用过NSNumber包装器吗?

Configure your store to use NSNumber, instead of float or decimal, and use this to save the coordinates:

将商店配置为使用NSNumber而不是float或decimal,并使用它来保存坐标:

[NSNumber numberWithDouble:coordinate.latitude]
//Same for longitude.

#2


1  

Try using the Double data type in Core Data. Since your location coordinates are doubles, makes sense to use the same in Core Data. Though that may be available just now (iOS 5).

尝试在Core Data中使用Double数据类型。由于您的位置坐标是双精度数,因此在核心数据中使用相同的位置是有意义的。虽然现在可以使用(iOS 5)。

#3


1  

When Core Data stores data in SQLite, it uses numeric columns. SQLite stores numbers as--at most--8-byte values, whether integer or floating-point. So, while an NSDecimalNumber would be quite happy to accurately represent these coordinate values, round-tripping them through a Core Data decimal attribute backed by SQLite will munge them.

当Core Data在SQLite中存储数据时,它使用数字列。 SQLite将数字存储为 - 最多 - 8字节值,无论是整数还是浮点数。因此,虽然NSDecimalNumber非常乐意准确地表示这些坐标值,但是通过SQLite支持的核心数据十进制属性对它们进行四舍五入将使它们变得非常好。

#4


0  

since coordinate ranges are well-defined, you can add a few digits of accuracy (compared to double) by using a 64 bit int representation (or even multiple).

由于坐标范围是明确定义的,因此可以使用64位int表示(甚至多次)来添加几位精度(与double相比)。

in reality, your sources and targets may not make use of or provide this much accuracy so... there may not be much to gain.

实际上,您的来源和目标可能无法使用或提供如此高的准确性,因此......可能没有太大的收获。

#5


0  

I believe the truncation problem could be on the logging, not on the actual data stored in Core Data.

我认为截断问题可能是日志记录,而不是存储在Core Data中的实际数据。

Can you confirm by posting the code you are using to log the output?

您可以通过发布用于记录输出的代码来确认吗?

I say this because I've noticed that when logging some of my longer NSString fields, Core Data description will only display ~50 characters or so, which might lead you to think it is truncating the data, but it's actually just truncating the description of it.

我这样说是因为我注意到在记录我的一些较长的NSString字段时,Core Data描述只会显示约50个字符左右,这可能会让你认为它正在截断数据,但它实际上只是截断了它。

#6


0  

In your data model editor, make sure the types for your lat/long fields are set to double. Otherwise CoreData will do the conversion for you, and the result will be not what you expect (in this case, the decimals will get dropped).

在数据模型编辑器中,确保lat / long字段的类型设置为double。否则CoreData将为您进行转换,结果将不是您所期望的(在这种情况下,小数将被删除)。

#7


0  

strongly recommend use NSDecialNumber for storing if you need precise value or compare in your code. you might have different value after store and get value back from core data in double fields.

如果您需要精确值或在代码中进行比较,强烈建议使用NSDecialNumber进行存储。存储后可能会有不同的值,并从双字段中的核心数据中获取值。