In a clean demo project I've created a MapView which is UIViewRepresentable and it all works great. It reads a geojson file with a polygon and draws it on the map. When I copy this exact code to my bigger project where i need this functionality I'm getting the following errors:
- Type 'MapView' does not confirm to protocol 'UIViewRepresentable'
- Value of type 'Context' as no member 'coordinator'
I don't understand why the standalone project compiles perfectly and integrated in my other project it doesn't. iOs versions are set the same
Can someone explain what is wrong ?
import SwiftUI
import MapKit
struct MapView: UIViewRepresentable {
var coordinate: CLLocationCoordinate2D
func makeUIView(context: Context) -> MKMapView {
let map = MKMapView()
map.mapType = .hybrid
map.delegate = context.coordinator
let region = MKCoordinateRegion( center: coordinate, latitudinalMeters: CLLocationDistance(exactly: 5000)!, longitudinalMeters: CLLocationDistance(exactly: 5000)!)
//map.setRegion(map.regionThatFits(region), animated: true)
map.centerCoordinate = coordinate
map.showsCompass = true
map.showsUserLocation = true
map.addOverlays(parseGeoJSON())
return map
}
func updateUIView(_ uiView: MKMapView, context: Context) {
let map = MKMapView()
map.setNeedsDisplay()
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
//MARK: Our delegate handler
final class Coordinator: NSObject, MKMapViewDelegate {
var control: MapView
init(_ control: MapView) {
self.control = control
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer{
if let polygon = overlay as? MKPolygon {
let renderer = MKPolygonRenderer(polygon: overlay as! MKPolygon)
renderer.fillColor = UIColor(red: 57 / 255, green: 255/255, blue: 20/255, alpha: 0.5)
//renderer.fillColor = UIColor(red: 192 / 255, green: 45/255, blue: 233/255, alpha: 0.5)
renderer.strokeColor = UIColor.black
renderer.lineWidth = 2
return renderer
}
return MKOverlayRenderer(overlay: overlay)
}
}
func parseGeoJSON() -> [MKOverlay]{
guard let url = Bundle.main.url(forResource: "zone", withExtension: "json") else {
fatalError("NOT FOUND")
}
var geoJson = [MKGeoJSONObject]()
do{
let data = try Data(contentsOf: url)
geoJson = try MKGeoJSONDecoder().decode(data)
} catch {
}
var overlays = [MKOverlay]()
for item in geoJson{
if let feature = item as? MKGeoJSONFeature {
for geo in feature.geometry{
if let polygon = geo as? MKPolygon{
overlays.append(polygon)
}
}
}
}
return overlays
}
}
struct ContentView: View {
var body: some View {
ZStack(alignment: .top) {
MapView(coordinate: CLLocationCoordinate2D(latitude:34.2226,longitude:-82.4592))
}
.ignoresSafeArea()
.navigationBarHidden(true)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}