Cannot convert value of type ... to expected. argument type CLLocationCoordinate2D

112 Views Asked by At

I'm trying to get a map view with annotations, and it seems like things have changed recently with iOS 17. I get an error with the below code in the No2 ADVANCED MAP section saying - Cannot convert value of type '[NationalParkLocation]' to expected argument type 'CLLocationCoordinate2D'

I'm fairly new to SwiftUI and would appreciate all the help I can get. Thanks!


import SwiftUI
import MapKit

struct MapView: View {

    @State private var cameraPosition: MapCameraPosition = {
        var mapCoordinates = CLLocationCoordinate2D(latitude: 6.600286, longitude: 16.4377599)

        var mapZoomLevel = MKCoordinateSpan(latitudeDelta: 70.0, longitudeDelta: 70.0)

        var mapRegion = MapCameraPosition.region(MKCoordinateRegion(center: mapCoordinates, span: mapZoomLevel))
        
        return mapRegion
    }()
    
    let locations: [NationalParkLocation] = Bundle.main.decode("locations.json")
    
    var body: some View {
        
        Map(position: $cameraPosition) {
            Annotation("Test", coordinate: locations) {
                Image(systemName: "mappin")
            }
        }
    }
}

I've tried this:


 Map(coordinateRegion: $cameraPosition, annotationItems: locations,
            annotationContent: { item in
            MapPin(coordinate: item.location, tint: .accent)
        })

But no luck. Plus, pretty sure this was deprecated.

I've updated the code now to this:


struct MapView: View {
    // MARK: - PROPERTIES
    
    @State private var cameraPosition: MapCameraPosition = {
        var mapCoordinates = CLLocationCoordinate2D(latitude: 6.600286, longitude: 16.4377599)
        var mapZoomLevel = MKCoordinateSpan(latitudeDelta: 70.0, longitudeDelta: 70.0)
        var mapRegion = MapCameraPosition.region(MKCoordinateRegion(center: mapCoordinates, span: mapZoomLevel))
        
        return mapRegion
    }()
    
    let locations: [NationalParkLocation] = Bundle.main.decode("locations.json")
    
    // MARK: - BODY
    
    var body: some View {
        Map(position: $cameraPosition) {
            ForEach(locations, id: \.id) { item in
                Marker(item.name, systemImage: "mappin.and.ellipse", coordinate: item.location)
            }
        }
    }
}

Everything is working as is now. Thanks.

0

There are 0 best solutions below