【问题标题】:how to save all CLlocations\CLLocationCoordinate2D into an array如何将所有 CLlocations\CLLocationCoordinate2D 保存到数组中
【发布时间】:2017-03-21 19:01:47
【问题描述】:

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

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 函数中——然后在这段代码的右下方,我试图抓取所有位置,以查看我的用户当前位置是否靠近任何 anootations,无论有多少。但相反,我只得到一个注释位置,因为当我将它存储在上面的 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
        }
    }

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

【问题讨论】:

    标签: arrays swift firebase cllocation cllocationcoordinate2d


    【解决方案1】:

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

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

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

    所以只需在上述方法中调用您的方法allLocationAnnotations()。 万事如意。

    【讨论】:

    • 感谢您的建议 :),但是由于从我的 firebase 中获取数据的代码是 databaseRef.child("Ice_Locations").queryOrderedByKey().observe(.childAdded,其中: {(快照)在该函数中,它作为一个循环运行,运行次数与我的 firebase 数据库中的子节点总数一样多,并且每当在数据库中添加新子节点时也会运行。Firebase 用作实时数据库。所以基本上,如果我将它添加到该函数中,它将永远循环
    猜你喜欢
    • 1970-01-01
    • 2013-09-25
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    相关资源
    最近更新 更多