加载现有的Geofire位置(如果存在) [英] Load existing Geofire location if one exists

查看:57
本文介绍了加载现有的Geofire位置(如果存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望Geofire做两件事之一;1-在Firebase中查找场地位置,如果不存在,则2-创建一个.

I would like Geofire to do one of two things; 1 - Look for a venue location in Firebase and if none exists, 2 - Create one.

因此要澄清一下:

  1. 用户根据当前地点创建一个帖子.
  2. Geofire会检查其是否已针对该地理位置记录了一个场所(长时坐标和纬度坐标).如果确实将该位置存储在Firebase中,则它将用户的新帖子和 ADDS 放置到同一地点.
  3. 如果Geofire 在Firebase中检测到位置,它将创建一个新的场所并将帖子附加到该场所.
  1. User creates a post based on the current venue.
  2. Geofire checks to see if it already has a venue (Long and Lat cocoordinates) on record for that geolocation. If it DOES have that location stored in Firebase then it takes the user's new post and ADDS it to the same venue.
  3. If Geofire DOES NOT detect a location in Firebase it creates a new venue and attaches the post to it.

我已经将场所详细信息与我的Geofire坐标分开了,但是我不明白如何让Geofire区分Firebase中是否存在该位置的条目.

I have separated my venue details from my Geofire coordinates but I dont understand how do I get Geofire to differentiate between whether an entry for this location does or does not exist within Firebase.

当我运行我的应用时,它会按照下面的屏幕抓取每次创建一个新条目,这显然不是我想要的.

At the moment when I run my app it creates a new entry every time as per the screen-grab below, which is obviously not what I want.

这是代码:

首先,我设置名称和目录的引用(我将它们分别设置,因为在少数情况下使用名称ref)

First I set the reference for the name and the directory (I set them seperately because I use the name ref in a few instances)

override func viewDidLoad() {
    super.viewDidLoad()

    geoFireNameRef = Database.database().reference().childByAutoId()
    geoFireRef = Database.database().reference().child("PostLocations").child("Coordinates")
    geoFire = GeoFire (firebaseRef: geoFireRef)
}

,然后在我的post方法中调用:

and then in my post method I call:

func savePost () {
     let location = CLLocation.init(latitude: lat!, longitude: long!) //Get Coordinates
     geoFire.setLocation(location, forKey: self.geoFireNameRef.key)// Save Coordinates 
postLocationDB.child(self.geoFireNameRef.key).setValue(locationDictionary) // Save Venue Details
}

推荐答案

因此,我了解到您想检查是否存在一个带有几个坐标的场地.我建议将这对夫妇存储在您的Coordinates/{venueId}节点下,作为由两个坐标组成的额外字段,如下所示:

So I understand that you want to check if there is a venue with an existing couple of coordinates. What I would suggest is to store this couple under your Coordinates/{venueId} node as an extra field which is composed by the two coordinates, like the following:

- Coordinates
   - -LAWR2LH.....
     - l
        - 0: 22.345
        - 1: 22.345
     - compoundL: "22.345-22.345"

,然后使用结合了queryOrderedByChild和queryEqualToValue的查询直接查询实时数据库,以查找是否已经存在具有compoundL = 22.345-22.345的Coordinates/Venue节点.

and then directly query the Real Time Database with a query that combines queryOrderedByChild and queryEqualToValue in order to find if a Coordinates/Venue node with compoundL = 22.345-22.345 is already existing.

这篇关于加载现有的Geofire位置(如果存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆