我知道类似的问题以前已经问过很多次了。只是找不到适合我需要的解决方案。请帮忙
我一直试图将信息从Firebase加载到mapview上。我能够附加所有信息视图图注解视图。但是我想要做的是单击地图注释视图时将Firebase数据传递给新的ViewController。单击批注时,我可以选择另一个ViewController,但是加载了错误的Firebase数据
到目前为止我尝试过的代码:
override func viewDidLoad() {
super.viewDidLoad()
self.mapView.delegate = self
locationmanager.delegate = self
locationmanager.desiredaccuracy = kCLLocationaccuracyBest
locationmanager.requestWhenInUseAuthorization()
locationmanager.requestLocation()
observeGeofire()
}
func observeGeofire() {
let dataRef = Database.database().reference().child("Shoutout")
dataRef.observeSingleEvent(of: .value,with: { (snapshot) in
for snap in snapshot.children {
let postsnap = snap as! Datasnapshot
if let dict = postsnap.value as? [String: AnyObject] {
let Lat = dict["lat"] as! CLLocationDegrees
let Long = dict["long"] as! CLLocationDegrees
let EVENT = dict["Event Type"] as? String
let Radmap = self.Rad
var annotations = MKPointAnnotation()
annotations.coordinate = CLLocationCoordinate2D(latitude: Lat,longitude: Long)
annotations.title = dict["title"] as? String
let loc = CLLocation(latitude: Lat,longitude: Long)
let distanceFromUser = (self.Location.distance(from: loc))/1000
let span = MKCoordinateSpanmake(0.25,0.25)
if distanceFromUser < Radmap && EVENT! == self.sovcCET {
self.mapView.addAnnotation(annotations)
print("testing text for \(self.sovcCET)")
} else if distanceFromUser < Radmap && self.sovcCET == "All Events"{
self.mapView.addAnnotation(annotations)
} else if distanceFromUser < Radmap && self.sovcCET == "Choose Event" {
self.mapView.addAnnotation(annotations)
} else {
print("failed test")
}
}
}
})
}
func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView?{
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
pinView = MKPinAnnotationView(annotation: annotation,reuseIdentifier: reuseId)
pinView?.pinTintColor = UIColor.red
pinView?.canShowCallout = true
let smallSquare = CGSize(width: 30,height: 30)
let button = UIButton(frame: CGRect(origin: CGPoint(x: 0,y :0),size: smallSquare))
let rightButton = UIButton(type: .contactAdd)
rightButton.tag = annotation.hash
pinView?.animatesDrop = true
pinView?.canShowCallout = true
pinView?.rightCalloutaccessoryView = rightButton
pinView?.leftCalloutaccessoryView = button
return pinView
}
func mapView(_ mapView: MKMapView,annotationView view: MKAnnotationView,calloutaccessoryControlTapped control: UIControl) {
//mapToDetails
if control == view.rightCalloutaccessoryView {
performSegue(withIdentifier: "mapToDetails",sender: self)
}
}
override func prepare(for segue: uistoryboardSegue,sender: Any?) {
if segue.identifier == "mapToDetails" {
if let destination = segue.destination as? shoutoutDetailViewController {
destination.shoutoutSelected = ShoutoutSelected
}
}
}
ShoutoutSelected实际上是包含多个变量的Shoutout类的实例,如下所示:
class Shoutout: NSObject {
// CLLocationDegrees
var event: String?
var when: String?
var Place: String?
var details: String?
var timestamp: Nsnumber?
var whenTimeStamp: Nsnumber?
var picsImageUrl: String?
var fromId: String?
var lat: CLLocationDegrees?
var long: CLLocationDegrees?
var title: String?
var subtitle: String?
var shoutoutId: String?
var distance: Double?
var tit: String?
var capt: String?
init(dictionary: [String: Any]) {
self.event = dictionary["Event Type"] as? String
self.when = dictionary["Date and Time"] as? String
self.whenTimeStamp = dictionary["Date and Time"] as? Nsnumber
self.Place = dictionary["place"] as? String
self.details = dictionary["details"] as? String
self.timestamp = dictionary["TIMESTAMP"] as? Nsnumber
self.picsImageUrl = dictionary["postPicUrl"] as? String
self.fromId = dictionary["FROM"] as? String
self.lat = dictionary["lat"] as? CLLocationDegrees
self.long = dictionary["long"] as? CLLocationDegrees
self.title = dictionary["title"] as? String
self.subtitle = dictionary["subtitle"] as? String
self.shoutoutId = dictionary ["ResponseID"] as? String
self.tit = dictionary ["Event Title"] as? String
self.capt = dictionary ["Captions"] as? String
}
func shouterPartnerId() -> String? {
return fromId
}
func soid() -> String? {
return shoutoutId
}
}
再次感谢