SwiftUI / Firebase : Displaying a pin on a map for each users' location

275 Views Asked by At

I would like to display multiple pins on a map. One for each of my users. The location of my users is stored on a Firebase database.

Here's my code :

import SwiftUI
import Firebase
import FirebaseFirestore
import CoreLocation
import MapKit

struct mapTimelineView: View {
    
    @StateObject private var locationViewModel = LocationViewModel.shared
            
    @ObservedObject var viewModel = TimelineViewModel()
    
    @ObservedObject var authViewModel = AuthViewModel()
    
    var body: some View {
        ZStack (alignment: .bottomTrailing) {
            
            GeoPointView(position: post.location)
        }
    }
}

struct GeoPointView : View {
    var position : GeoPoint
    
    struct IdentifiablePoint: Identifiable {
        var id = UUID()
        var position : GeoPoint
    }
    
    @StateObject private var locationViewModel = LocationViewModel.shared

    var body: some View {
        Map(coordinateRegion: $locationViewModel.region, showsUserLocation: true, annotationItems: [position].map { IdentifiablePoint(position: $0)}) { point in
                MapMarker(coordinate: CLLocationCoordinate2D(latitude: point.position.latitude, longitude: point.position.longitude), tint: Color("accentColor"))
            }
    }
}

post.location retrieves the location data from the database.

I have tried to use a ForEach loop (ForEach(viewModel.posts) { post in [...code here...] }) but it did not work.

I have no errors in my code but I can't put the markers for each user. So I would like to know : How can I put different pins according to the location of my users ?

0

There are 0 best solutions below