我正在尝试获取单个事件的详细信息,以在新的视图控制器中实例化它们,但是当我点击出现在屏幕上的注释时,我似乎无法弄清楚在打印事件名称时我怎么走地图,感谢您的帮助
MapViewController :
class MapViewController: UIViewController {
var userRides: [UserRides] = []
@IBOutlet weak
var HostButton: UIButton!
@IBOutlet weak
var ViewButton: UIButton!
@IBOutlet weak
var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
getRides()
NotificationCenter.default.addObserver(self, selector: #selector(refreshRides), name: Notification.Name("refresh"), object: nil)
}
@objc func refreshRides(notification: NSNotification) {
getRides()
}
func getRides() {
let db = Firestore.firestore()
db.collection("Events").getDocuments() {
(snapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
let documents = snapshot!.documents
try !documents.forEach {
document in
let userRides: UserRides =
try document.decoded()
print(userRides.rideName)
self.userRides.append(userRides)
}
for ride in 0.. < self.userRides.count {
let mapPins = EventAnno()
let rides = self.userRides[ride]
mapPins.title = rides.rideName
mapPins.myRide = rides
let coordinate = CLLocationCoordinate2D(latitude: rides.rideYCordinate, longitude: rides.rideXCordinate)
mapPins.coordinate = coordinate
self.mapView.addAnnotation(mapPins)
print(self.userRides.count)
}
}
}
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let eventAnnotation = view.annotation as ? EventAnno {
let theEvent = eventAnnotation.myRide
print(theEvent ? .rideName ? ? "ride name")
}
}
}
class EventAnno: MKPointAnnotation {
var myRide: UserRides ?
}
UserRides :
struct UserRides: Decodable{
let rideDate : String
let rideLeader : String
let rideLeaderUID : String
let rideLocation : String
let rideName : String
let rideType: String
let rideXCordinate : Double
let rideYCordinate : Double
}
------------------------------编辑---------------- --------------
以下是我在 MapViewController 上进行的第二次尝试,试图从另一个角度进行尝试,但仍然没有结果。
class MapViewController: UIViewController{
var userRides:[UserRides] = []
@IBOutlet weak var HostButton: UIButton!
@IBOutlet weak var ViewButton: UIButton!
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
getRides()
NotificationCenter.default.addObserver(self, selector: #selector(refreshRides), name: Notification.Name("refresh"), object: nil)
mapView.delegate = self
}
@objc func refreshRides (notification: NSNotification){
getRides()
}
func getRides(){
let db = Firestore.firestore()
db.collection("Events").getDocuments() { (snapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
let documents = snapshot!.documents
try! documents.forEach{document in
let userRides: UserRides = try document.decoded()
print(userRides.rideName)
self.userRides.append(userRides)
}
for ride in 0..<self.userRides.count {
let mapPins = MKPointAnnotation()
let rides = self.userRides[ride]
mapPins.title = rides.rideName
let coordinate = CLLocationCoordinate2D(latitude: rides.rideYCordinate, longitude: rides.rideXCordinate)
mapPins.coordinate = coordinate
self.mapView.addAnnotation(mapPins)
print(self.userRides.count)
}
}
}
}
}
extension MapViewController: MKMapViewDelegate {
func annotateMap() {
var parent: MapViewController func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier="Placemark" var annotationView=mapView.dequeueReusableAnnotationView(withIdentifier: identifier) if annotationView==nil {
annotationView=MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier) annotationView?.canShowCallout=true annotationView?.rightCalloutAccessoryView=UIButton(type: .detailDisclosure)
}
else {
annotationView?.annotation=annotation
}
return annotationView
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
print("\(String(describing: view.annotation?.title))")
}
}
}
答案 0 :(得分:0)
这是从Firebase获取管脚数据的工作代码,我的问题是我在weakVar上引用了不正确的地图,请检查您的变量!
import UIKit
import MapKit
import Firebase
import FirebaseAuth
class MapViewController: UIViewController{
var userRides:[UserRides] = []
@IBOutlet weak var HostButton: UIButton!
@IBOutlet weak var ViewButton: UIButton!
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
getRides()
NotificationCenter.default.addObserver(self, selector: #selector(refreshRides), name: Notification.Name("refresh"), object: nil)
mapView.delegate = self
}
@objc func refreshRides (notification: NSNotification){
getRides()
}
func getRides(){
let db = Firestore.firestore()
db.collection("Events").getDocuments() { (snapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
let documents = snapshot!.documents
try! documents.forEach{document in
let userRides: UserRides = try document.decoded()
print(userRides.rideName)
self.userRides.append(userRides)
}
for ride in 0..<self.userRides.count {
let mapPins = MKPointAnnotation()
let rides = self.userRides[ride]
mapPins.title = rides.rideName
let coordinate = CLLocationCoordinate2D(latitude: rides.rideYCordinate, longitude: rides.rideXCordinate)
mapPins.coordinate = coordinate
self.mapView.addAnnotation(mapPins)
}
}
}
}
}
extension MapViewController: MKMapViewDelegate{
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
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.pinTintColor = .purple
pinView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
else {
pinView!.annotation = annotation
}
return pinView
}
}