【问题标题】:ONLY Show annotations for venues within radius仅显示半径范围内场地的注释
【发布时间】:2018-03-14 04:49:56
【问题描述】:

我试图只显示离我的用户最近的位置的注释。我已将所有位置保存到 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 { 返回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)
}

【问题讨论】:

  • 您是否将纬度和经度值保存在不同位置的数据库中?如果是,您可以将结果设置为使用比较器,例如一次获取所有数据意味着所有对象,现在设置用户当前位置的范围并将该范围与 dataSnapshot 对象纬度和经度进行比较并填充数据
  • 不,它们在同一个位置。
  • 我更新了代码以显示我是如何设置的
  • 当我看到纬度和经度值存在于 Db 和您当前的位置时,所以您只想使用 DB 中与当前用户位置匹配的具有相同纬度和经度的值填充 TableView 是不是?
  • 我想在地图和表格中填充用户 1 公里或 1 英里范围内的位置。

标签: swift firebase mapkit mkannotation clcircleregion


【解决方案1】:

因为我的位置与您的位置不同。我在设备上对其进行了测试,结果就像与我所在位置的距离一样,而您的场地位置大约为 3K 。因此您可以使用该代码文件并在您的设备中检查它,这将有所帮助。

1) 我现在已将您的 didload 函数替换为不同的函数,并在地图的 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)")
                }

            }
        }
    }

如果还有问题请留言

【讨论】:

  • 我现在去看看,
  • 不幸的是,它没有用。没有弹出坐标。给我一些测试位置,我会放进去。
  • 我没有在您使用的 tableView 数组中填充数据我只是在那里编写了一些代码,这将导致输出,您必须使用它附加到数组中并将该数组填充到 TableView ,得到我的观点或我应该修改一些代码以供更多参考?
  • 请多参考。我不确定解决方案。
  • 好的,我会根据我的位置创建一个示例项目,并让你知道结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
  • 2014-04-27
  • 2020-10-19
  • 2018-09-25
  • 2018-11-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多