0

So I have this Cloud Firestore Setup, where each of the Image Documents have the same 4 fields, but with different values.

How can I then iterate over all of the Image Docs and get the GeoPint? So I later can display the Latitude and Longitude?

I Guess the iteration part isn’t THAT important, so antoher way of asking is, how can I get the Geopoint and then display it in my project?

enter image description here

Nick
  • 219
  • 4
  • 15

1 Answers1

2

Building on my last answer (https://stackoverflow.com/a/66377922/560942):

struct ImageModel {
  var id = UUID()
  var quote : String
  var url: String
  var position: GeoPoint
} 
images = snapshot.documents.compactMap { documentSnapshot -> ImageModel? in
  let documentData = documentSnapshot.data()
  if let quote = documentData["Quote"] as? String,
    let url = documentData["Url"] as? String,
    let position = documentData["Position"] as? GeoPoint
      {
        return ImageModel(quote: quote, url: url, position: position)
      } else {
        return nil
      }
    }

Then, to display the text coordinates later on:

ForEach(images, id: \.id) { image in 
  Text("Location: \(image.position.latitude), \(image.position.longitude)")
}

Update for displaying the GeoPoint:

struct GeoPointView : View {
    var position : GeoPoint

    struct IdentifiablePoint: Identifiable {
        var id = UUID()
        var position : GeoPoint
    }
    
    var body: some View {
        Map(coordinateRegion: .constant(
                MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: position.latitude, longitude: position.longitude), span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005))), annotationItems: [position].map { IdentifiablePoint(position: $0)}) { point in
            MapPin(coordinate: CLLocationCoordinate2D(latitude: point.position.latitude, longitude: point.position.longitude))
        }
            .frame(width: 200, height: 200)
    }
}

Usage:

ForEach(images, id: \.id) { image in 
  Text("Location: \(image.position.latitude), \(image.position.longitude)")
  GeoPointView(position: image.position)
}

In the event you mean on a map, I can update this answer if needed, or you can refer to the following post on the Apple Developer Forums: https://developer.apple.com/forums/thread/651668

jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • When passing "position" into "ImageModel" what type should "position" have in that struct? And how can i display the position on a map? @jnpdx – Nick Feb 26 '21 at 10:07
  • Position would be of type GeoPoint. Did you follow the link to the Apple developer post? Was there a specific part of it that I can clarify if I update my answer? – jnpdx Feb 26 '21 at 14:29
  • Updated my answer with an example of a map showing the GeoPoint – jnpdx Feb 26 '21 at 20:09
  • I tried the code and all of it seems to work as it should. But how can i modify the "map code" so I just dipsplay one hardcoded position? And show my own location as well @jnpdx – Nick Feb 26 '21 at 22:36
  • If you want to display one hardcoded position, you could write in one specific latitude and longitude. Look at where those are passed to the map in the `CLLocationCoordinate2D` clauses. Showing your own location as well is out of the scope of this question as it involves asking for location permission, etc, but there are plenty of resources out there about it. For example: https://stackoverflow.com/questions/25296691/get-users-current-location-coordinates – jnpdx Feb 26 '21 at 22:43
  • I see, but some of the "map code" is now unnecessary since im no longer iterating over the data fetched from the database, but im not sure what i should delete @jnpdx – Nick Feb 26 '21 at 22:50
  • Just call it like this `GeoPointView(position: GeoPoint(latitude: 0.0, longitude: 0.0))` and keep everything the same. Replace the 0.0s with the location you want to highlight. – jnpdx Feb 26 '21 at 22:53
  • Okay yeah, that works. Thanks for the help @jnpdx – Nick Feb 26 '21 at 23:04
  • A last question i have is, how can i display a simple map without any points or specific locations provided @jnpdx – Nick Feb 26 '21 at 23:08
  • Call `Map` without the `annotationItems` or the trailing closure -- `Map(coordinateRegion: .constant(...))` and fill in the `constant` with the code I provided before. – jnpdx Feb 26 '21 at 23:10
  • Im not entirely sure i understand. But its not that important right now. I just realised that the positions are displayed in different maps, how can i display multiple positions in the same map @jnpdx – Nick Feb 26 '21 at 23:21
  • Turn `var position` into an array of GeoPoints. If you need more than that, like yesterday, I'm going to suggest you ask another question. Your original question has answered and rather than adding more questions in the comments that aren't in the scope of the original question, please create a new question. – jnpdx Feb 26 '21 at 23:24
  • Will do thats probably for the best, https://stackoverflow.com/questions/66394069/swiftui-how-to-display-multiple-pins-on-map @jnpdx – Nick Feb 26 '21 at 23:33