I'm trying to only show the annotations for locations nearest to my user. I have all the locations already saved to firebase and now I'm just trying to retrieve them and populate the Map and TableView with only the locations around the user. I've tried GeoFire and a few others but, I'm sure I'm not doing this right. Could any one help?
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()
})
}
Here is how I set up my table:
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)
}