I have searched this site and StackOVerflow forums and Im unable to find an answer that helps me figure out how to add MapMarkers to my MapView.
I am adding onto the SwiftUI Landmarks Tutorial,
I want to be able to see the marker when i click on the landmark row name and the map shows up for that location and i want to have a map view that shows MapMarkers of all locations in that area.
This is my MapView
import SwiftUI
import MapKit
struct MapView: View {
var coordinate: CLLocationCoordinate2D
var landmark: Landmark
@State private var region = MKCoordinateRegion()
var body: some View {
Map(coordinateRegion: $region)
.onAppear {
setRegion(coordinate)
MapMarkerTool(coordinate: landmark.locationCoordinate)
}//onappear
}//body.view
private func setRegion(_ coordinate: CLLocationCoordinate2D) {
region = MKCoordinateRegion(
center: coordinate,
span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
}//private func
}//struct mapview
struct MapView_Previews: PreviewProvider {
static var previews: some View {
MapView(coordinate:CLLocationCoordinate2D(latitude: 36.4786, longitude: -117.0745), landmark: landmark)
}//view
}//preview
This is my MapMarkerTool
import SwiftUI
import MapKit
struct MapMarkerTool: Identifiable {
let id: UUID
let location: CLLocationCoordinate2D
init(id: UUID = UUID(), lat: Double, long: Double) {
self.id = id
self.location = CLLocationCoordinate2D(
)
}
}
struct PinAnnotationMapView: View {
let place: MapMarkerTool
@State var region: MKCoordinateRegion
var body: some View {
Map(coordinateRegion: $region,
annotationItems: [place])
{ place in
MapMarker(coordinate: place.location,
tint: Color.purple)
}
}
}
//Prieview was deleted because i wasn't sure if it was needed or not and it kept showing up as an error.