I have several users locations stored inside of my Firestore database, and called them all to a variable, and then I mapped both of them into a map annotation called Marker(coordinate: CLLocationCoordinate2D(latitude: realLat, longitude: realLong))
. When I call the map:
Variables (called before the view): @State var realLong = 0.00
and @State var realLat = 0.00
The $viewmodel.region
can be anything, I've just set it to a location button.
The Map:
Map(coordinateRegion: $viewModel.region, showsUserLocation: true, annotationItems: markers) { markers in
MapAnnotation(coordinate: markers.coordinate) {
Circle()
}
}
Firebase Function Code:
func downloadMapData() {
let db = Firestore.firestore()
db.collection("annotations").addSnapshotListener {(snap,
err) in
if err != nil{
print("\(String(describing: err))")
return
}
for i in snap!.documentChanges {
_ = i.document.documentID
if let latCoordinate = i.document.get("lat") as?
Double {
DispatchQueue.main.async {
realLat = latCoordinate
print("realLat: \(realLat)")
}
}
}
for i in snap!.documentChanges {
_ = i.document.documentID
if let longCoordinate = i.document.get("long") as?
Double {
DispatchQueue.main.async {
realLong = longCoordinate
print("realLong: \(realLong)")
}
}
}
}
}
It shows up nowhere near the real place. I think that the coordinates might be smushed together.
Inside of the console I get this:
realLat: 37.3348892
realLat: 69.69
realLong: -122.0088363
realLong: 42.32
It look that if there might be 2 instances of the variables???
Is there any way I could make separate annotations for each off the users inside of my database, with each user getting their own map annotation? Thanks in advance!