我是编程新手,因此对我的任何无知表示歉意。我也确信我没有遵循许多最佳实践,但我的问题更具体。我正在为特定人群制作联系人应用程序。该应用程序的一部分是 map View ,显示每个联系人的引脚。当我保存一个新人时,我正在对地址进行地理编码,如下所示:
let entity = NSEntityDescription.entity(forEntityName: "erson", in: self.managedObjectContext)
let record = NSManagedObject(entity: entity!, insertInto: self.managedObjectContext)
geocoder.geocodeAddressString(rvAddress, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error!)
}
if let placemark = placemarks?.first {
let rvCoordinates = placemark.location!.coordinate
let rvLatitude = rvCoordinates.latitude
let rvLongitude = rvCoordinates.longitude
record.setValue(rvLatitude, forKey: "latitude")
record.setValue(rvLongitude, forKey: "longitude")
}
})
在从初始 TableView 到 map View Controller viewController.fetchedPeople = fetchedResultsController.fetchedObjects 到 var fetchedPeople: [Person]? 在 map View Controller 类中。我在 map View Controller 中有一个这样的 for 循环:
for people in fetchedPeople! {
let firstName = people.value(forKey: "firstName") as? String
let lastName = people.value(forKey: "lastName") as? String
let street = people.value(forKey: "street") as? String
let rvLatitude = people.value(forKey: "latitude") as? Double
let rvLongitude = people.value(forKey: "longitude") as? Double
print("LAT: \(rvLatitude!) LONG: \(rvLongitude!)")
let rvCoordinates = CLLocationCoordinate2D(latitude: rvLatitude!, longitude: rvLongitude!)
var personName: String!
var personAddress: String!
if firstName != nil {
personName = firstName! + " " + lastName!
} else {
personName = lastName!
}
print(personName)
personAddress = street!
let mapPerson = MapPerson(title: personName, locationName: personAddress, coordinate: rvCoordinates)
//mapPeopleArray.append(mapPerson)
mapView.addAnnotation(mapPerson)
print(rvCoordinates)
当我最初添加联系人时,一切都很好。引脚按预期下降。控制台显示:
LAT: 41.1656913 LONG: -81.8692497
Eric Madzay
CLLocationCoordinate2D(latitude: 41.165691299999999, longitude: -81.869249699999997)
LAT: 41.253647 LONG: -81.875974
Ethan Madzay
CLLocationCoordinate2D(latitude: 41.253647000000001, longitude: -81.875973999999999)
LAT: 41.203748 LONG: -81.858518
Work
CLLocationCoordinate2D(latitude: 41.203747999999997, longitude: -81.858518000000004)'
但是,当我关闭应用程序、重新运行并打开 map View 时,只有一些图钉掉落。我对任何事情都进行零更改。只需关闭/打开并导航即可查看。控制台读取:
LAT: 41.253647 LONG: -81.875974
Ethan Madzay
CLLocationCoordinate2D(latitude: 41.253647000000001, longitude: -81.875973999999999)
LAT: 41.1656913 LONG: -81.8692497
Eric Madzay
CLLocationCoordinate2D(latitude: 41.165691299999999, longitude: -81.869249699999997)
LAT: 0.0 LONG: 0.0
Work
CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
这只是一个例子,但似乎最好地展示了我的问题。我知道我一定是遗漏了一些东西或不了解某些东西是如何运作的,我一生都无法弄清楚自己。感谢您的宝贵时间。
Best Answer-推荐答案 strong>
谢谢 pbasdf,这是我不明白的,完成处理程序是如何工作的。将上下文也保存在地理编码 block 中解决了我的问题。谢谢!
关于ios - 从核心数据中检索 Swift 中的坐标时遇到问题,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/44729173/
|