How to store CLLocation using Core Data (iPhone)?

前端 未结 5 1909
故里飘歌
故里飘歌 2021-01-30 01:41

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

5条回答
  •  礼貌的吻别
    2021-01-30 02:17

    The "best" way to store it depends on what you want to do with it:

    • If you want the "same" CLLocation, serialize it. NSKeyedUnarchiveFromData is fine.
    • If you just want to search on latitude and longitude, then store those as doubles (and check the "indexed" checkbox).

    There's a bunch of additional properties you can save/restore if you do it manually (altitude, horizontalAccuracy, verticalAccuracy, timestamp). There are some more that you can't (speed, heading); CLLocation doesn't provide a suitable init-method and the properties are readonly.

    All of the extra properties are useful if you're recording a track. Altitude is useful if you're recording a POI on mountainous terrain ("we still have to climb 100 m"). horizontal/vertical accuracy might be used to represent how big the POI is (e.g. a city might have a "horizontal accuracy" of several km and be displayed as a big circle).

提交回复
热议问题