我正在从我的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函数中-然后在此代码的正下方,我试图获取所有位置,以查看我的用户当前位置是否在任何阳极附近。但是相反,我只能得到一个注释位置,因为当我将其存储在上面的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
}
}
另外,当我尝试追加时,我只能像以前一样获得一个注释位置。再次感谢您,希望能对您有所帮助:)
最佳答案
您仅获得一个注释,因为您的方法allLocationAnnotations()
仅被调用一次。
位置更新时,每次调用的唯一方法是
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
}
因此,只需在上述方法中调用方法
allLocationAnnotations()
即可。祝一切顺利 。
关于arrays - 如何将所有CLlocations\CLLocationCoordinate2D保存到数组中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42936004/