[英]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.