[英]Calculating distance between two PFGeoPoints in Swift
我正在尝试查找两个PFGeoPoint之间的距离(以英里为单位)。 用户的当前位置以及在Parse中保存的位置。 在我的代码中,我正在检索保存的位置并将其存储在itemLocation中。
之后,我立即使用geoPointForCurrentLocationInBackground查找用户的当前位置,并将两者之间的距离与distanceInMiles进行比较,并将值分配给名为distance的变量。
保存所有这些之后,“解析”中的“距离”字段仍然变为空白,因此未保存。 我在这里做错了什么?
func resolveItemInfo(){
var items = PFObject(className: "Items")
var query = PFQuery(className: "Items")
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects {
for (index, title) in enumerate(objects) {
let itemLocation = (title as! PFObject)["location"] as! PFGeoPoint
PFGeoPoint.geoPointForCurrentLocationInBackground({ (geoPoint: PFGeoPoint?, error: NSError?) -> Void in
if error == nil {
var userLocation = geoPoint
var distance = userLocation?.distanceInMilesTo(itemLocation)
items.setValue(distance, forKey: "distance")
items.saveInBackgroundWithBlock({ (success: Bool, error: NSError?) -> Void in
if success {
} else {
println("Error")
}
})
}
})
1。 这是如何调试的想法,(希望对您的编码事业有所帮助)
在代码块中设置一些“断点”,您可能会发现“ geoPointForCurrentLocationInBackground”下的代码块永远不会被调用。
现在,您知道为什么不保存PFObject了。
下一步。 搜索为什么geoPointForCurrentLocationInBackground永远不会运行
您需要将以下键/值对添加到应用的info.plist中:
<key>NSLocationWhenInUseUsageDescription</key>
<string>We want to send your location data to the NSA, please allow access to location services. (replace this text with whatever you want the dialog to say)</string>
参考文献:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.