从 Parse 转换 PFGeopoint 纬度和经度以将当前用户与其他用户的半径距离显示到文本标签上

Convert PFGeopoint lat and long from Parse to display current users distance in radius to other users onto a text label

第一张图片是返回结果的示例,第二张图片是结果页面的示例,'distance' 是我需要更改以显示用户距离的标签。我将所有用户位置存储在 Parse 上,作为 PFGeoPoint,在纬度和经度中称为 "location"。然后我有一个带有 textLabel 的 tabelViewCell。所有用户都显示在 VC 上,我试图显示这些用户与当前用户的差距,就像在 Tinder 中一样。

我在日志中将其他用户位置 运行 作为纬度和经度坐标,并且我将文本标签从 "distance" 更新为“[] 公里远!”所以我必须取回数组,但它返回的是空的。

我已经在互联网上搜索过,但似乎无法弄明白。所有的教程都是obj c或者json或者在mapView中添加注解。这是我的 usersResultsViewController 上的代码:

var locationManager : CLLocationManager!

var latitude: CLLocationDegrees = 0
var longitude: CLLocationDegrees = 0

@IBAction func done(sender: UIBarButtonItem) {
    self.performSegueWithIdentifier("backToProfile", sender: self)

}

@IBOutlet var resultsPageTableView: UITableView!

var imageFiles = [PFFile]()
var instrumentText = [String]()
var nameText = [String]()
var ageText = [String]()
var locationText = [PFGeoPoint]()

var usersLocations = Double

让 roundedTwoDigitDistance = Double

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()

    // start of tableView:
let query = PFQuery(className: "_User")
    query.whereKey("username", notEqualTo:PFUser.currentUser()!.username!)
    query.findObjectsInBackgroundWithBlock { (users: [AnyObject]?, error: NSError?) -> Void in

if error == nil {
// success

print(users!.count)
for user in users! {
self.imageFiles.append(user["image"] as! PFFile)
self.instrumentText.append(user["instrument"] as! String)
self.nameText.append(user["name"] as! String)
self.ageText.append(user["age"] as! String)
// self.locationText.append(user["location"] as! PFGeoPoint)

}
// reload the table forget this it will load nothing  
    self.resultsPageTableView.reloadData()

} else {
    print("error")
    }
    }
    }
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    // *** note to self: for the return here it must be a mandatory field for user look at this again nd change it to mandatory age or username or something.

    return imageFiles.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let singleCell: CustomCell = tableView.dequeueReusableCellWithIdentifier("mySingleCellid") as! CustomCell

    // text
    singleCell.usersInstrument.text = instrumentText[indexPath.row]
    singleCell.userName.text = nameText[indexPath.row]
    singleCell.userAge.text = ageText[indexPath.row]

let query = PFUser.query()!

    if let latitude = PFUser.currentUser()?["location"]?.latitude {

        if let longitude = PFUser.currentUser()?["location"]?.longitude {

            print(latitude)
            print(longitude)

            query.whereKey("username", notEqualTo:PFUser.currentUser()!.username!)
            query.whereKey("location", withinGeoBoxFromSouthwest: PFGeoPoint(latitude: latitude - 10, longitude: longitude - 10), toNortheast:PFGeoPoint(latitude:latitude + 10, longitude: longitude + 10))

            query.findObjectsInBackgroundWithBlock { (users: [AnyObject]?, error: NSError?) -> Void in

                if error == nil {
                    // success

                    for user in users! {

        singleCell.userDistance.text = "\(self.locationText) km away!"

这里有一些我觉得有用的论坛,但我仍然卡住了!!!:

http://www.scriptscoop.com/t/a2d00e357960/ios-converting-a-pfgeopoint-lat-and-long-from-parse-into-a-cllocation-lat-.html Two Query Constraints On One Key with Parse and Swift

使用 "near" 功能并开始获取 1 英里内的所有用户,然后保存该信息,然后获取 2 英里内的所有用户,然后是 5 英里等。继续设置地理围栏围绕特定位置并随着增加 radius/distance 在同心圆中增长,您可以保存每个用户的估计距离。

还可以查询包含在特定区域内的一组对象。要在矩形边界框中查找对象,请将 withinGeoBox 限制添加到您的 Parse.Query.

var query = new Parse.Query(PlaceObject);
query.withinMiles("location", userGeoPoint, 10.0);
query.find().then(function(placesObjects) {
  // Get a list of objects within 10 miles of a user's location
});

更多示例位于 https://www.parse.com/docs/js/guide

PFGeopoints 有称为 "distanceInMilesTo:" 和 "distanceInKilometersTo:" 的方法。这些是您将要使用的。在存储当前用户位置的 PFGeopoint 上调用该方法,并从您的查询中传入每个用户的位置。将结果存储在适当的标签中。

这是此方法的 link 到 API 参考:http://parse.com/docs/ios/api/Classes/PFGeoPoint.html#//api/name/distanceInKilometersTo: