Post

Replies

Boosts

Views

Activity

Reply to Adding MapPin to SwiftUI Tutorial MapView.swift
The complete MapView.swift with all of the changes noted above, which are the only changes to the downloaded project identified above: import SwiftUI import MapKit struct MapView: View {     var coordinate: CLLocationCoordinate2D     @State private var region = MKCoordinateRegion()     struct Place: Identifiable {         let id = UUID()         let name: String         let coordinate: CLLocationCoordinate2D     }     var annotations = [         Place(name: "Xyz", coordinate: CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude))     ]     var body: some View {         Map(coordinateRegion: $region, annotationItems: annotations) {             MapPin(coordinate: $0.coordinate)         }         .onAppear {             setRegion(coordinate)         }     }     private func setRegion(_ coordinate: CLLocationCoordinate2D) {         region = MKCoordinateRegion(             center: coordinate,             span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)         )     } } struct MapView_Previews: PreviewProvider {     static var previews: some View {         MapView(coordinate: CLLocationCoordinate2D(latitude: 34.011_286, longitude: -116.166_868))     } }
Oct ’21
Reply to Adding MapPin to SwiftUI Tutorial MapView.swift
All of the code is in the SwiftUI tutorial at the URL provided above. Start with the BuildingListsAndNavigation.xcodeproj in the Complete folder of the downloaded project. I have just now repeated the changes shown above starting with a new download, with the same results. Note that the addition goes before the View, which is completely replaced.
Replies
Boosts
Views
Activity
Oct ’21
Reply to Adding MapPin to SwiftUI Tutorial MapView.swift
The complete MapView.swift with all of the changes noted above, which are the only changes to the downloaded project identified above: import SwiftUI import MapKit struct MapView: View {     var coordinate: CLLocationCoordinate2D     @State private var region = MKCoordinateRegion()     struct Place: Identifiable {         let id = UUID()         let name: String         let coordinate: CLLocationCoordinate2D     }     var annotations = [         Place(name: "Xyz", coordinate: CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude))     ]     var body: some View {         Map(coordinateRegion: $region, annotationItems: annotations) {             MapPin(coordinate: $0.coordinate)         }         .onAppear {             setRegion(coordinate)         }     }     private func setRegion(_ coordinate: CLLocationCoordinate2D) {         region = MKCoordinateRegion(             center: coordinate,             span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)         )     } } struct MapView_Previews: PreviewProvider {     static var previews: some View {         MapView(coordinate: CLLocationCoordinate2D(latitude: 34.011_286, longitude: -116.166_868))     } }
Replies
Boosts
Views
Activity
Oct ’21
Reply to Menu Picker .onChange (doesn't work for the first selection)
Nice fix for this problem. The purpose of this example was getting the .onChange to work correctly to call a function for selecting multiple values. Thanks for the help.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’22