如何将所有CLlocations \ CLLocationCoordinate2D保存到数组中

时间:2017-03-21 19:01:47

标签: arrays swift firebase cllocation cllocationcoordinate2d

我从firebase数据库中检索所有Annotation位置。下面的代码(仅在将新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函数中 - 然后在此代码中右下方我试图抓住所有位置,看看我的用户当前位置是否接近任何一个动画,无论多少。但相反,我只获得一个注释位置,因为当我将它存储在上面的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
        }
    }

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

1 个答案:

答案 0 :(得分:0)

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

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

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

}

所以只需在上述方法中调用您的方法allLocationAnnotations()即可。 一切顺利。