从Parse转换PFGeopoint lat和long,以将当前用户的半径距离显示给文本标签上的其他用户

时间:2015-11-01 15:55:50

标签: swift parse-platform location distance

result page not showing distance

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

我在日志中运行其他用户位置作为lat和long坐标,我从"距离"更新了文本标签。到[" []公里远!"所以我必须得到数组,但它返回空。

我搜索了互联网,似乎无法弄明白。所有教程都是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 trying to access the subscript of a Parse query array in SWIFT Two Query Constraints On One Key with Parse and Swift

2 个答案:

答案 0 :(得分:1)

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

以下是此方法的API参考的链接:http://parse.com/docs/ios/api/Classes/PFGeoPoint.html#//api/name/distanceInKilometersTo

答案 1 :(得分:0)

使用"附近"功能并开始让所有用户在1英里内,然后保存该信息,然后让所有用户在2英里内,然后5英里内等。继续在特定位置周围放置地理围栏,并在半径增加的同心圆中生长/ 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