Trying to Display Location Data from Firebase to Mapkit

Load all firebase data into mapView

You need to get all your events from your Events collection and then loop trough all events and add them.

   let db = Firestore.firestore()
db.collection("Events").getDocuments { (snapshot, error) in
if let documents = snapshot?.documents {
for document in documents {
let coordinates = CLLocationCoordinate2D(latitude: (value["latitude"] as! CLLocationDegrees), longitude: value["longitude"] as! CLLocationDegrees)
let event = Event(title: value["title"] as? String, locationName: value["location"] as? String, discipline: value["discipline"] as? String, coordinate: coordinates)
self.mapView.addAnnotation(event)
}
}
}

How to fetch location markers from firebase realtime database and add to map view in Xcode with swift

you can simply replace your old tableView forRowAt func with

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "countCell") as! countCell
let nightClub: NightClubs
nightClub = nightClubs[indexPath.row]

let artwork = Artwork(title: nightClub.name,
locationName: nightClub.location,
discipline: nightClub.type,
coordinate: CLLocationCoordinate2D(latitude: Double(nightClub.latitude)!, longitude: Double(nightClub.longitude)!)
)
mapView.addAnnotation(artwork)
cell.goingCountLabel.text = nightClub.goingCount
cell.liveCountLabel.text = nightClub.liveCount
return cell
}

then remove your hardcoded Artwork objects and its relations

Swift 3 Firebase to MapKit

Using MKPointAnnotation() will only place a pin on the map. You need to use a custom class to hold the annotation information and call it with initializers.

Original:

let annotation = MKPointAnnotation()
annotation.coordinate = pinCoordinate
self.mapContainerView.addAnnotation(annotation)

Working version:

let annotation = PinAnnotation(title: MTA, coordinate: pinCoordinate, info: MTA)
annotation.coordinate = pinCoordinate
self.mapContainerView.addAnnotation(annotation)

Also Need:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
}

and

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
}

Populate Map With Firebase data

Add the following code after print(lat,long)

print(lat,long)
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2DMake(lat, long)
annotation.title = "Hello!" // if you need title, add this line
self.mapView.addAnnotation(annotation)


Related Topics



Leave a reply



Submit