Swift 仅显示半径范围内场馆的注释
我试图只显示离我的用户最近的位置的注释。我已经将所有位置保存到firebase,现在我只是尝试检索它们,并仅使用用户周围的位置填充地图和TableView。我试过GeoFire和其他一些,但我肯定我做得不对。有人能帮忙吗Swift 仅显示半径范围内场馆的注释,swift,firebase,mapkit,mkannotation,clcircleregion,Swift,Firebase,Mapkit,Mkannotation,Clcircleregion,我试图只显示离我的用户最近的位置的注释。我已经将所有位置保存到firebase,现在我只是尝试检索它们,并仅使用用户周围的位置填充地图和TableView。我试过GeoFire和其他一些,但我肯定我做得不对。有人能帮忙吗 因为我的位置和你的位置不同。我在设备上测试了它,结果是距离我的位置和你们的场地位置大约3公里。因此,您可以使用该代码文件并在设备中检查它,这将有所帮助 1) 我现在已经将您的didload函数替换为另一个func,并在地图的diddupdateLocation中调用它 2)
因为我的位置和你的位置不同。我在设备上测试了它,结果是距离我的位置和你们的场地位置大约3公里。因此,您可以使用该代码文件并在设备中检查它,这将有所帮助 1) 我现在已经将您的didload函数替换为另一个func,并在地图的diddupdateLocation中调用它 2) 将//iosGeek用作我在文件中插入代码的代码行上方的注释 3) 由于位置的差异,目前我并没有在地图或表格中填充数据
Link: https://drive.google.com/open?id=0Bz8kF1Gedr7fNThTbTFZY1Q3LVk
在FeedVc中查看此函数
1) 请在设备上测试以获得位置差异
Link: https://drive.google.com/open?id=0Bz8kF1Gedr7fNThTbTFZY1Q3LVk
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)")
}
}
}
}
如果snapshot.exists()存在{
如果让snapshot=snapshot.children.allObjects为?[DataSnapshot]{
用于管理单元快照{
//这里我只是用你的快照来过滤数据
self.postData=snap.value as![String:AnyObject]
//使用自定义函数计算此处的距离
让距离=self.calculateDistancxe(userlat:self.userLatt,userLon:self.userLonn,venueLat:self.postData[“纬度”]!as!CLLocationDegrees,venueLon:self.postData[“纬度”]!as!CLLocationDegrees)
//如果距离小于或等于2 km
设aa:Int=Int(距离)!
如果(aa)您是否在不同位置的数据库中保存纬度和经度值?如果是,您可以使用比较器(如“一次获取所有数据”表示所有对象)生成结果,现在从用户当前位置设置一个范围,并将该范围与dataSnapshot对象纬度和经度进行比较并填充数据否它们位于同一位置在上。我更新了代码以显示我是如何设置的,因为我看到纬度和经度值在Db和您的当前位置中,所以您只想用Db中与当前用户位置匹配的纬度和经度相同的值填充TableView,是吗?我想用1km或1mi内的位置填充地图和表格用户的le。我现在要检查,不幸的是,它不起作用。没有弹出坐标。给我一些测试位置,我会把它们放进去。我没有在你的tableView数组中填充数据,我只是在那里写了一些代码,将产生输出,你必须使用它附加到数组中,并将该数组填充到tableView,得到我的观点或我应该修改一些代码以供更多参考?请更多参考。我不确定解决方案。好的,我将根据我的位置创建一个示例项目,并让您知道结果
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)")
}
}
}
}