如何使用 Core Data (iPhone) 存储 CLLocation? [英] How to store CLLocation using Core Data (iPhone)?

查看:25
本文介绍了如何使用 Core Data (iPhone) 存储 CLLocation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存一个位置,然后使用 Core Location、MapKit 和 Core Data 框架在地图上检索该位置.

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

我所做的只是创建了名为 POI 的实体,并添加了一些属性,例如纬度(双精度型)、经度(双精度型)以及其他一些属性.

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.

简单地说,我的应用使用两个 NSNumber 保存 POI.(经纬度)但我觉得必须有比这更聪明的方式来存储 CLLocation.

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.

干杯.

推荐答案

你做的很好.您应该在核心数据中将纬度和经度保存为双精度值.当您需要再次获取信息时,从 Core Data 中获取双精度数据,并使用 CLLocationCoordinate2DMake 之类的函数构造一个 CLLocationCoordinate2D 结构.没有内置存储位置的方法,因此存储纬度和经度分量就可以了.

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.

这篇关于如何使用 Core Data (iPhone) 存储 CLLocation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆