user1574598
user1574598

Reputation: 3881

Setting the zoom level for MKMap in SwiftUI - Xcode 11

I am new to SwiftUI, and after a tutorial I am trying to set the initial zoom level for the map as its zoomed out very far.

I have been reading through this but its not making much sense I can't seem to find a line of code that specifies an int or a double that specifies the zoom (e.g. in Google Maps API it is the case)

I tried changing my MKCoordinateRegion with MKCoordinateRegionMakeWithDistance and setting the third arg as a let zoom = CLLocationDistance(10) but this does not work.

Here is my code:

import SwiftUI
import MapKit

struct MapView: UIViewRepresentable {
    func makeUIView(context: UIViewRepresentableContext<MapView>) -> MKMapView {

        MKMapView()
    }

    func updateUIView(_ uiView: MKMapView, context: UIViewRepresentableContext<MapView>) {

        // set coordinates (lat lon)
        let coords = CLLocationCoordinate2D(latitude: 53.062640, longitude: -2.968900)

        // set span (radius of points)
        let span = MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)

        // set region
        let region = MKCoordinateRegion(center: coords, span: span)

        // set the view
        uiView.setRegion(region, animated: true)
    }
}

Upvotes: 6

Views: 10090

Answers (1)

Tobias Hesselink
Tobias Hesselink

Reputation: 1647

This delta values changes the zoom level.

// set span (radius of points)
let span = MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5)

If you change 0.5 to 0.01, the map will zoom in. Take a high value like 0.8, the map will zoom out.

Hope I provided enough information. Good luck!

Upvotes: 24

Related Questions