0

我从我的 firebase 数据库中检索所有注释位置。下面的代码(仅在将新子项添加到数据库后运行)

var ILocation = CLLocationCoordinate2D()
let manager = CLLocationManager()
var UserName = String()
var C1 = CLLocation()

func allLocationAnnotations() {
        let databaseRef = FIRDatabase.database().reference()

        databaseRef.child("Locations").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in
            let value = snapshot.value as? NSDictionary
            let longitude = value?["Longitude"] as! String
            let latitude = value?["Latitude"] as! String
            let name = value?["Location Shared By"] as! String
            let annotationCityName = value?["Annotation_City"] as! String

            self.allAnnotationLocations.insert(annotationStruct(Longitude: longitude, Latitude: latitude), at: 0)
//            print("\(name) - Location of Longitude \(longitude), Latitude: \(latitude)")

            let annotation = MKPointAnnotation()

            annotation.title = "\(name)"
            annotation.subtitle = "around this location"

            annotation.coordinate.latitude = Double(latitude)!
            annotation.coordinate.longitude = Double(longitude)!
            self.map.addAnnotation(annotation)
            self.addRadiusCircle(location: annotation)

            self.TotalInUserCity += 1
            self.SpottedNum.text = "\(self.TotalInUserCity)"

            self.C1 = CLLocation(latitude: Double(latitude)!, longitude: Double(longitude)!)
        })
    }

然后我使用这个 locationManager 来获取用户位置

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations[0]
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        ILocation = myLocation
        self.map.showsTraffic = true
        self.map.isZoomEnabled = true
        self.map.showsUserLocation = true

        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
        map.setRegion(region, animated: true)

        geoCoder.reverseGeocodeLocation(location, completionHandler: { placemarks, error in
            guard let addressDict = placemarks?[0].addressDictionary else {
                return
            }

            if let city = addressDict["City"] as? String {
                self.CityLocation.text = "\(city)"
//                print("City is: \(city)")
            }
        })

下面的代码在同一个 locationManager 函数中——然后在这段代码的右下方,我试图抓取所有位置,以查看我的用户当前位置是否靠近任何一个注解,无论有多少。但相反,我只得到一个注释位置,因为当我将它存储在上面的 var C1 中时,它只能保存一个我知道的值,但是当我尝试创建一个数组并将它们保存在里面时,我会得到像 can' 这样的错误t 将 Cllocation 转换为 CLLocationCoordinate2D :( 提前感谢您的帮助

    let C2 = CLLocation(latitude: Double(self.ILocation.latitude), longitude: Double(self.ILocation.longitude))

    let distanceInMeters = C1.distance(from: C2) // result is in meters

    if(distanceInMeters <= 1609)
    {
        print("Your are close to a Location Shared by:---------------")
    }
    else
    {
        // out of 1 mile
    }
}

这是一件代码

    var ILocation = CLLocationCoordinate2D()
    let manager = CLLocationManager()
    var UserName = String()
    var C1 = CLLocation()

    func allLocationAnnotations() {
            let databaseRef = FIRDatabase.database().reference()

            databaseRef.child("Locations").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in
                let value = snapshot.value as? NSDictionary
                let longitude = value?["Longitude"] as! String
                let latitude = value?["Latitude"] as! String
                let name = value?["Location Shared By"] as! String
                let annotationCityName = value?["Annotation_City"] as! String

                self.allAnnotationLocations.insert(annotationStruct(Longitude: longitude, Latitude: latitude), at: 0)
    //            print("\(name) - Location of Longitude \(longitude), Latitude: \(latitude)")

                let annotation = MKPointAnnotation()

                annotation.title = "\(name)"
                annotation.subtitle = "around this location"

                annotation.coordinate.latitude = Double(latitude)!
                annotation.coordinate.longitude = Double(longitude)!
                self.map.addAnnotation(annotation)
                self.addRadiusCircle(location: annotation)

                self.TotalInUserCity += 1
                self.SpottedNum.text = "\(self.TotalInUserCity)"

                self.C1 = CLLocation(latitude: Double(latitude)!, longitude: Double(longitude)!)
            })
        }

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let location = locations[0]
            let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
            ILocation = myLocation
            self.map.showsTraffic = true
            self.map.isZoomEnabled = true
            self.map.showsUserLocation = true

            let span:MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1)
            let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
            map.setRegion(region, animated: true)

            geoCoder.reverseGeocodeLocation(location, completionHandler: { placemarks, error in
                guard let addressDict = placemarks?[0].addressDictionary else {
                    return
                }

                if let city = addressDict["City"] as? String {
                    self.CityLocation.text = "\(city)"
    //                print("City is: \(city)")
                }
            })

        let C2 = CLLocation(latitude:   Double(self.ILocation.latitude), longitude: Double(self.ILocation.longitude))

        let distanceInMeters = C1.distance(from: C2) // result is in meters

        if(distanceInMeters <= 1609)
        {
            print("Your are close to a Location Shared by:---------------")
        }
        else
        {
            // out of 1 mile
        }
    }

此外,当我尝试追加时,我只能像以前一样获得一个注释位置。再次感谢您希望您能提供帮助:)

4

1 回答 1

0

你只得到一个注释,因为你的方法allLocationAnnotations()只被调用一次。

当位置更新时,每次调用的唯一方法是

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

}

所以只需在上面的方法中调用你allLocationAnnotations()的方法。一切顺利 。

于 2017-03-21T19:37:35.310 回答