我试图只显示离我的用户最近的位置的注释。我已将所有位置保存到 firebase,现在我只是想检索它们并仅使用用户周围的位置填充 Map 和 TableView。我已经尝试过 GeoFire 和其他一些方法,但我确定我做的不对。有人可以帮忙吗?
override func viewDidLoad() {
super.viewDidLoad()
// Set up Map
mapView.delegate = self
mapView.userTrackingMode = MKUserTrackingMode.follow
// Setup TableView
tableView.delegate = self
tableView.dataSource = self
//Pulls TableData for UITableView
DataService.ds.REF_VENUE.observe(.value, with: { (snapshot) in
self.posts = [] // THIS IS THE NEW LINE
if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshot {
if let postDict = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let post = Post(postKey: key, postData: postDict)
self.posts.append(post)
}
//Populates Map with annotations.
if let locationDict = snap.value as? Dictionary<String, AnyObject> {
let lat = locationDict["LATITUDE"] as! CLLocationDegrees
let long = locationDict["LONGITUDE"] as! CLLocationDegrees
let title = locationDict["NAME"] as! String
let center = CLLocationCoordinate2D(latitude: lat, longitude: long)
_ = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.20, longitudeDelta: 0.20))
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2DMake(lat, long)
annotation.title = title.capitalized
self.mapView.addAnnotation(annotation)
}
}
}
self.tableView.reloadData()
})
}
这是我设置桌子的方式:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return posts.count }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "detailCell", for: indexPath) as? PostCell {
let cellData = posts[indexPath.row]
cell.configureCell(post: cellData)
return cell
} else {
return PostCell()
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let post = posts[indexPath.row]
performSegue(withIdentifier: "previewSegue", sender: post)
}