将位置坐标保存到Core数据

编程入门 行业动态 更新时间:2024-10-24 04:31:17
本文介绍了将位置坐标保存到Core数据-Swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将位置坐标保存到Core Data中-存储这些坐标并检索它们的正确方法是什么?

I want to save location coordinates into Core Data- what is the right way of storing these and retrieving them?

此刻,我正在这样做并遇到错误:

At the moment I am doing this and getting errors:

var newPlaceCoordinate = CLLocationCoordinate2D() var newPlaceLatitude = CLLocationDegrees() var newPlaceLongitude = CLLocationDegrees()

我通过使用自动完成小部件使用Google Maps API来获得坐标。

I get my coordinates from using Google maps API using the autocomplete widget.

let newPlaceCoordinate = place.coordinate self.newPlaceCoordinate = place.coordinate let newPlaceLatitude = place.coordinate.latitude print(newPlaceLatitude) self.newPlaceLatitude = place.coordinate.latitude let newPlaceLongitude = place.coordinate.longitude print(newPlaceLongitude) self.newPlaceLongitude = place.coordinate.longitude

然后存储我使用的坐标:

Then to store the coordinates I use:

let appDelegate = UIApplication.shared.delegate as! AppDelegate let context = appDelegate.persistentContainer.viewContext let newPlace = NSEntityDescription.insertNewObject(forEntityName: "StoredPlace", into: context) newPlace.setValue(newPlaceCoordinate, forKey: "coordinate") newPlace.setValue(newPlaceLatitude, forKeyPath: "latitude") newPlace.setValue(newPlaceLongitude, forKeyPath: "longitude")

我将所有属性都设置为String类型。 然后在我的ViewDidLoad中进行检索:

And I have the attributes all set as String types. Then to retrieve in my ViewDidLoad I have:

let appDelegate = UIApplication.shared.delegate as! AppDelegate let context = appDelegate.persistentContainer.viewContext let request = NSFetchRequest<NSFetchRequestResult>(entityName: "StoredPlace") request.returnsObjectsAsFaults = false if let coordinate = result.value(forKey: "coordinate") as? String { let latitude = (coordinate as NSString).doubleValue let longitude = (coordinate as NSString).doubleValue let markers = GMSMarker() markers.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) markers.icon = GMSMarker.markerImage(with: UIColor.yellow) markers.tracksViewChanges = true markers.map = vwGMap }

因此,这无法正常工作并产生属性:属性错误 =坐标;所需的类型= NSString;给定类型= NSConcreteValue。我有几个问题。

So this is not working and producing an error of 'attribute: property = "coordinate"; desired type = NSString; given type = NSConcreteValue'. I have a few questions.

如何正确保存坐标数据? 是否将其另存为CLLocationDegrees或CLLocationCoordinate2D? 如何将这些对象转换为NSString或在属性中使用其他类型? (我尝试将其更改为Double或Integer,但也会产生错误)。我有多个位置坐标,我想存储和从核心数据中检索。

How do I save a coordinate data appropriately? Do I save it as a CLLocationDegrees or CLLocationCoordinate2D? How do I convert these objects into NSString or should I be using a different type in my attributes? (I tried changing it to Double or Integer but it also produced errors). I have multiple location coordinates which I would want to store and retrieve from core data.

推荐答案

如果您要处理大量数据,那么CoreData是最好的选择。

If you are going to deal with lots of data, then CoreData is the best bet.

这些是我在 StorePlace 实体中的属性,最好仅保存纬度和经度,并在实现中需要时从中创建坐标。

These are my attributes in StorePlace entity, its best to save only latitude and longitude and create coordinates from them when needed in your implementation.

@NSManaged public var newPlaceLatitude: Double @NSManaged public var newPlaceLongitude: Double

然后要插入新数据,

class func insert(latitude: Double, longitude: Double) { let appDelegate = UIApplication.shared.delegate as! AppDelegate let managedObjectContext = appDelegate.managedObjectContext let entity = NSEntityDescription.entity(forEntityName: "StoredPlace", in:managedObjectContext) let newItem = StoredPlace(entity: entity!, insertInto: managedObjectContext) newItem.newPlaceLatitude = latitude newItem.newPlaceLongitude = longitude do { try managedObjectContext.save() } catch { print(error) } }

检索经度和纬度之后,您可以在实现中使用以下坐标:

After retrieving latitude, longitude you can use coordinate in your implementations as

let newItem = getCoordinateFromCoreData() // your retrieval function let coordinate = CLLocationCoordinate2D(latitude: newItem.latitude, longitude: newItem.longitude)

更多推荐

将位置坐标保存到Core数据

本文发布于:2023-11-26 17:48:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634568.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:坐标   位置   数据   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!