Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

map example

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

>Solution :

You’re setting the latitude to both the latitude and longitude (in two spots).

longitude: vineyard.location.coordinate.latitude

Should be .longitude.

Resulting coordinate, (38.65680447392557, 38.65680447392557) is in Turkey, as shown.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading