Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing high precision latitude/longitude numbers in iOS Core Data

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

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?

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}
like image 989
Bryan Avatar asked Jan 03 '11 14:01

Bryan


3 Answers

Have you used the NSNumber wrapper?

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

[NSNumber numberWithDouble:coordinate.latitude]
//Same for longitude.
like image 130
Evan Mulawski Avatar answered Oct 13 '22 11:10

Evan Mulawski


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).

like image 36
Stephen C Avatar answered Oct 13 '22 11:10

Stephen C


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.

like image 40
Sixten Otto Avatar answered Oct 13 '22 13:10

Sixten Otto