views:

262

answers:

1

Here is my situation. For some reason my annotation coordinates that are saved in core data do not seem to match the coordinates that I can retrieve from the pins location on the map.

So, I save my data into core data using something like:

[ride setLatitude:[NSNumber numberWithDouble:newRidePlacemark.coordinate.latitude]];
[ride setLongitude:[NSNumber numberWithDouble:newRidePlacemark.coordinate.longitude]];

I am pretty positive that is the right format to save the long and lat in. But, the issue appears when I compare the coordinate that I have in core data with the coordinate of the dropped pin. I use the method and log to see what location the pin is at:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

NSLog(@"%f, %f", view.annotation.coordinate.latitude, view.annotation.coordinate.longitude);

This gives me something like:

38.119432, -122.225647

But, at that same time, I am checking what is saved in core data for that pin, and I have:

38.11943249695161, -122.2256469726562

Where did that difference come from? Why was one shortened? This is a real problem, because when I try to remove annotations, the coordinates do not match, and the annotation does not get removed.

Have I missed something here?

+2  A: 

First you need to ensure that you are using double types (64 bit) all the way through your data flow. If the data temporarily get converted into a float (32 bits) you will loose precision. If you store in core data with a plist for back-end, you may actually have it temporily converted into some other data representation such as text string.

Second you should be careful when comparing floating point numbers in general. Instead of comparing that the values are equal you should allow for some tiny differences depending on the precision you want, e.g.

if(fabs(num1 - num2) <= 0.000001) {
// numbers are considered equal
}

Hope this helps.

Claus Broch
Ok, it seems that I am keeping the number as a double the entire time. The number itself does not get changed, it just gets shortened.
Nic Hubbard
Sounds like it may be an issue with your core data schema not storing as 64 bit floating point, but 32 bit.
Claus Broch
But I am saving them as double, not integer 32 or 64.
Nic Hubbard