我正在尝试从所有Firestore文档中检索航班号,纬度和经度,并将它们作为Mapbox注释添加到mapView中。到目前为止,代码已从Cloud Firestore中提取数据并将其存储为变量。附加地,该代码显示带有坐标的Mapbox地图,但是必须在数组中手动分配
在将Firestore中的变量附加到坐标数组时遇到麻烦。
我发现这个"Add annotation after retrieving users latitude and longitude from Firebase"沿正确的路线,但是与firebase而不是firestore有关。
任何帮助将不胜感激!
class HomeViewController: UIViewController, MGLMapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.styleURL = MGLStyle.darkStyleURL
mapView.tintColor = .lightGray
mapView.centerCoordinate = CLLocationCoordinate2D(latitude: 0, longitude: 66)
mapView.zoomLevel = 2
mapView.delegate = self
view.addSubview(mapView)
var aircraftArray = [""]
let db = Firestore.firestore()
let AircraftRef = db.collection("LiveAircraftData").getDocuments { (snapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in snapshot!.documents {
let FlightNumber = document.documentID
let latitude = document.get("Latitude") as! Double
let longitude = document.get("Longitude") as! Double
print(FlightNumber, latitude, longitude)
var Coordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
aircraftArray.append(Coordinates)
var pointAnnotations = [MGLPointAnnotation]()
for coordinate in Coordinates {
let point = MGLPointAnnotation()
point.coordinate = coordinate
point.title = "\(coordinate.latitude), \(coordinate.longitude)"
pointAnnotations.append(point)
}
mapView.addAnnotations(pointAnnotations)
}
}
}
}