How to store CLLocation using Core Data (iPhone)?

bicbac picture bicbac · Sep 26, 2010 · Viewed 21.7k times · Source

I'm trying to save a location and retrieve the location on a map afterward using Core Location, MapKit and Core Data frameworks.

What I've done is I just made entity named POI and added properties such as latitude (double type), longitude (double type) with few others.

Simply put, my app saves POI with two NSNumbers. (lat and long) but I feel like there must be a smarter way to store CLLocation than that.

cheers.

Answer

nevan king picture nevan king · Sep 26, 2010

What you're doing is fine. You should save the latitude and longitude as doubles in Core Data. When you need to get the information again, get the doubles back from Core Data and construct a CLLocationCoordinate2D struct with a function like CLLocationCoordinate2DMake. There's no built in way to store a location, so storing the latitude and longitude components is fine.

If you're not doing any math operations on the latitude or longitude (finding bounding boxes etc) you could store them as strings. Floats and doubles can change their values slightly, making comparison operations fail.