@robnotyou quick question.
Since MKMapView doesnt require a binding to region, my map shows empty blue (probably in the middle of the sea).
@State private var region = MKCoordinateRegion()
MapView(region: region)
.edgesIgnoringSafeArea(.all)
.onAppear() {
region = ... array of CLLocationCoordinate2D
}
This used to work when it was Map since i just had to go $region since the parameter required has to be binding but now it is not. How can it re-render once the region variable is set?
This is my MapView
import SwiftUI
import MapKit
struct MapView: UIViewRepresentable {
var region: MKCoordinateRegion
var polylineCoordinates: [CLLocationCoordinate2D]?
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView()
mapView.delegate = context.coordinator
mapView.region = region
if let polylines = polylineCoordinates {
let polyline = MKPolyline(coordinates: polylines, count: polylines.count)
mapView.addOverlay(polyline)
}
return mapView
}
func updateUIView(_ view: MKMapView, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
}
class Coordinator: NSObject, MKMapViewDelegate {
var parent: MapView
init(_ parent: MapView) {
self.parent = parent
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if let routePolyline = overlay as? MKPolyline {
let renderer = MKPolylineRenderer(polyline: routePolyline)
renderer.strokeColor = UIColor.systemBlue
renderer.lineWidth = 5
return renderer
}
return MKOverlayRenderer()
}
}