我正在尝试按位置对用户的帖子进行排序并使用 PFGeopoint 和 Swift 显示距离
Posted
技术标签:
【中文标题】我正在尝试按位置对用户的帖子进行排序并使用 PFGeopoint 和 Swift 显示距离【英文标题】:I'm Trying to sort a user's post by location and displaying the distance using PFGeopoint and Swift 【发布时间】:2017-02-13 23:53:29 【问题描述】:我正在尝试按距离对用户的帖子进行排序,并隐藏任何超过一定里程的帖子。我还试图在我的 UITableview 中从最近到最远进行排序:
class FindAParty:UITableViewController
//var partyData:NSMutableArray! = NSMutableArray()
var partyData = [PFObject]()
//var user:NSMutableArray = NSMutableArray()
override init(style: UITableViewStyle)
super.init(style: style)
required init?(coder aDecoder: NSCoder)
super.init(coder: aDecoder)
//fatalError("init(coder:) has not been implemented")
@IBAction func loadData()
PFGeoPoint.geoPointForCurrentLocation (geopoint, error) in
print(geopoint)
if let geopoint = geopoint
PFUser.current()?.remove(forKey: "Location")
print ("True")
PFUser.current()? ["Location"] = geopoint
PFUser.current()?.saveInBackground()
print ("Load Data went through")
partyData.removeAll()
print ("Remove ALL Objeccts")
let findPartyData = PFQuery(className: "Party")
print("PFQuery...")
findPartyData.findObjectsInBackground
(objects:[PFObject]?, error:Error?)->Void in
if error != nil
print(error!)
else
if let objects = objects
self.partyData = objects.reversed()
DispatchQueue.main.async
self.tableView.reloadData()
override func viewDidAppear(_ animated: Bool)
self.loadData()
print("View Did Appear")
override func viewDidLoad()
super.viewDidLoad()
print("ViewDidLoad")
//self.loadData()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
// #pragma mark - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int
return 1
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return partyData.count
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FindAPartyCell
let party = self.partyData[indexPath.row]
cell.typeOfPartyLabel.alpha = 0
cell.timeOfPartyLabel.alpha = 0
cell.usernameLabel.alpha = 0
cell.alcoholTFLabel.alpha = 0
cell.typeOfPartyLabel.text = party.object(forKey: "partyTitle") as? String
cell.timeOfPartyLabel.text = party.object(forKey: "partyTime") as? String
cell.usernameLabel.text = party.object(forKey: "Usernames") as? String
cell.alcoholTFLabel.text = party.object(forKey: "ATF") as? String
// var dataFormatter:NSDateFormatter = NSDateFormatter()
//dataFormatter.dateFormat = "yyyy-MM-dd HH:mm"
//cell.timestampLabel.text = dataFormatter.stringFromDate(sweet.createdAt)
/* let findUser:PFQuery = PFUser.query()!
findUser.whereKey("objectId", equalTo: party.object(forKey: "Username")!)
findUser.findObjectsInBackground
(objects:[PFObject]?, error: Error?) -> Void in // Changes NSError to Error
if error == nil
let user:PFUser = (objects)!.last as! PFUser
cell.usernameLabel.text = user.username
*/ UIView.animate(withDuration: 0.5, animations:
cell.typeOfPartyLabel.alpha = 1
cell.timeOfPartyLabel.alpha = 1
cell.usernameLabel.alpha = 1
cell.alcoholTFLabel.alpha = 1
)
//
//
return cell
如果可能的话,我还想显示距离。 谢谢你的建议
【问题讨论】:
【参考方案1】:您可以使用 Parse 提供的地理查询。
whereKey:nearGeoPoint
返回按距离排序的对象数组(从最近到最远)
whereKey:nearGeoPoint:withinMiles
/ whereKey:nearGeoPoint:withinKilometers
使用距离(米/公里)限制结果
-
按“位置”查询用户(您应该考虑使用小写的属性)
let query = PFUser.query()!
query.whereKey("Location", nearGeoPoint: geopoint)
获取他们的帖子...(单独的帖子表?使用关系?)
【讨论】:
以上是关于我正在尝试按位置对用户的帖子进行排序并使用 PFGeopoint 和 Swift 显示距离的主要内容,如果未能解决你的问题,请参考以下文章
Google place 自动完成 api 按位置对结果进行排序