如何使用 Core Data (iPhone) 存储 CLLocation?

How to store CLLocation using Core Data (iPhone)?(如何使用 Core Data (iPhone) 存储 CLLocation?)
本文介绍了如何使用 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
How to store value, received in delegate class, inside external one?(如何存储在委托类中接收的值,在外部类中?)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)