ios – How to properly display coordinates on MapKit Map in SwiftUI


I’m working on a map that displays the location of a vineyard that makes a particular wine and am unable to display negative coordinates.

The coordinates for a winery in northern California I got from Google maps are: (38.65680447392557, -122.84383931534359) but when I display them like so:

struct VineyardMapView: View {
    @State var vineyard: Vineyard
    @State var mapCamera: MapCameraPosition = MapCameraPosition.automatic
    
    var body: some View {
        //How to zoom map out??
        Map(position: $mapCamera) {
            Marker(coordinate: CLLocationCoordinate2D(latitude: vineyard.location.coordinate.latitude, longitude: vineyard.location.coordinate.latitude), label: {Image(systemName: "mappin")})
        }
        .mapStyle(.imagery)
        .frame(width: 350, height: 400)
        .clipShape(RoundedRectangle(cornerRadius: 25.0))
        .onAppear {
            //load Map Camera position at runtime
            mapCamera = MapCameraPosition.region(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: vineyard.location.coordinate.latitude, longitude: vineyard.location.coordinate.latitude), span: MKCoordinateSpan(latitudeDelta: 10, longitudeDelta: 10)))
        }
    }
}

#Preview {
    VineyardMapView(vineyard: Vineyard("Jordan",
                                       CLLocation(latitude: 38.6562, longitude: -122.8438),
                                       "https://www.jordanwinery.com/"))
}

It places me somewhere in the middle east:
Ex:

map example

Can someone explain how to properly display coordinates both positive and negative on a mapkit map in swiftUI?

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img