Hello together,
i do have the following View showing and and marking a given location on a map.
import SwiftUI
import MapKit
struct MapView: View {
var poi: Ballpark?
var body: some View {
if let poi = poi {
Map(initialPosition: .region(MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: poi.location!.latitude, longitude: poi.location!.longitude),
span: MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02))))
{
Marker("\(poi.name)", systemImage: "mappin", coordinate: CLLocationCoordinate2D(latitude: poi.location!.latitude, longitude: poi.location!.longitude))
}
.ignoresSafeArea()
.mapStyle(.hybrid)
}
}
}
the coordinates of the ballpark are part of the Ballpark struct
struct Ballpark: Identifiable, Codable, Comparable, Hashable {
@DocumentID var id: String? = UUID().uuidString // -> UUID of firestore document
var name: String
var street: String
var nr: Int?
var ext: String?
var city: String
var zip: Int?
var googleMaps: String
var category: String?
var location: GeoPoint? //= Coordinate(latitude: 0.0, longitude: 0.0)
var imageUrl: String?
}
i do read the ballpark informations from a firestorm database and the user can choose the requested ballpark. once this is done, the Form-View should show the position of the ballpark centered in the Map-View.
Th interesting thing is, that after selecting a new ballpark, the marker changes, but the view does not center to the new location. So the new location coordinates are passed into the MapView(). Does anyone of you has any idea, what’s wrong?
@State private var stadium: Ballpark?
Form {
Section(header: Text("Select Your Home Ballpark")) {
Picker ("Ballpark", selection: $stadium) {
if stadium == nil {
Text("select ballpark").tag(Optional<Ballpark>(nil))
}
ForEach(model.ballparks.sorted()) { arg in
Text(arg.name).tag(Optional(arg))
}
}
.pickerStyle(.menu)
}
MapView(poi: stadium)
.cornerRadius(10)
.padding(.all)
}
the whole code is way too much and too complex, therefore I just add the screenshots of my app.
Start situation:
select new ballpark:
after selection: marker changed, but map didn’t center to new position
Thanks and best regards
Peter