查询获取未执行的附近位置

时间:2017-02-03 07:14:16

标签: ios iphone swift3 firebase-storage geofire

我正在使用geofire进行位置保存。我的代码附在下面,      位置保存代码:

    let usersRoot = ref.child("geograpgicalLocationDetails")
    let geoFire = GeoFire(firebaseRef: usersRoot.child(userID!))

    geoFire?.setLocation(CLLocation(latitude: (self.currentLocation?.latitude)!, longitude:(self.currentLocation?.longitude)!), forKey:"location") { (error) in
        if (error != nil) {
            print("An error occured: \(error)")
        } else {
            print("Saved location successfully!")
        }
    }

Nearby location fetching:            

        var allKeys = [String:CLLocation]()

        let query = geoFire?.query(at: center, withRadius: 0.001)
        print("about to query")
        query?.observe(GFEventType.init(rawValue: 0)!, with: {(key: String?, location: CLLocation?) in
            print("Key: \(key), location \(location?.coordinate.latitude)")
            allKeys [key!] = location
        })

我的问题是位置提取查询没有执行,所以我无法得到结果。请帮帮我

2 个答案:

答案 0 :(得分:0)

let ref = FIRDatabase.database().reference()
let geoFire = GeoFire(firebaseRef: ref.child("users_location"))

func findNearbyUsers() {

    if let myLocation = myLocation {

        let theGeoFire = GeoFire(firebaseRef: ref.child("users_location"))
        let circleQuery = theGeoFire!.query(at: myLocation, withRadius: radiusInMeters/1000)

        _ = circleQuery!.observe(.keyEntered, with: { (key, location) in

            if !self.nearbyUsers.contains(key!) && key! != FIRAuth.auth()!.currentUser!.uid {
                self.nearbyUsers.append(key!)
            }

        })

        //Execute this code once GeoFire completes the query!
        circleQuery?.observeReady({

            for user in self.nearbyUsers {

                self.ref.child("users/\(user)").observe(.value, with: { snapshot in
                    let value = snapshot.value as? NSDictionary
                    print(value)
                })
            }

        })

    }

}

试试这个。

答案 1 :(得分:0)

使用以下功能:

func getaddressFromGoogle(_ position:CLLocationCoordinate2D,completion:@escaping (String)->()) {

    let url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=\(position.latitude),\(position.longitude)&key=\(your api key)"
    print(url)
    Alamofire.request(url, method: .get, parameters: nil, headers: ["Accept": "application/json", "Content-Language": "en"])
    .responseJSON { response in

        print(response.response as Any) // URL response
        print(response.result.value as AnyObject)   // result of response serialization
        //  to get JSON return value
        if let result = response.result.value as? [String:AnyObject] {
            if let status = result["status"] as? String, status == "OK" {
                if let results = result["results"] as? [[String:AnyObject]] {
                    print(results)
                    if let result = results.first {
                        print(result)
                        if let address = result["formatted_address"] as? String {
                            completion(address)
                        }
                    }
                }
            }
        }
    }
}