我试图仅显示离我的用户最近的位置的注释。我已将所有位置保存到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) - &gt; 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)
}
答案 0 :(得分:1)
因为我的位置与您的位置不同。我在设备上进行了测试,结果就像距离我的位置一样,你的场地位置大约是3K。所以你可以使用该代码文件并在你的设备中检查它将有所帮助。
1)我已经将你的didload函数替换为另一个func,并在地图的DidUpdateLocation中调用它
2)使用// iosGeek作为注释代码,我将代码插入到文件中。
3)现在由于位置差异,我没有在地图或表格中填充数据
Link: https://drive.google.com/open?id=0Bz8kF1Gedr7fNThTbTFZY1Q3LVk
在FeedVc中查看此功能
1)请在设备上测试以获得位置差异
2)在控制台中打印位置以检查其是否成立
3)在If else条件下打印self.postdata以检查输出内容
if snapshot.exists(){
if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshot {
//Here I just used your snapshot to filter data
self.postData = snap.value as! [String : AnyObject]
//calculating distance here with custom function made
let distance = self.calculateDistancxe(userlat: self.userLatt, userLon: self.userLonn, venueLat: self.postData["LATITUDE"]! as! CLLocationDegrees, venueLon: self.postData["LATITUDE"]! as! CLLocationDegrees)
//if Distance is less Than or equal to 2 km
let aa : Int = Int(distance)!
if (aa <= 2){
//if yes , add *self.postData* in a new Dictionary or Array
print("*********\(self.postData)")
//time to use that postdata in a dict or array from which you will populate data in TableView
}
else{
//if condition don't satisfy
print("noting \(distance)")
}
}
}
}
评论是否仍有问题