monkeyUser
monkeyUser

Reputation: 4661

Change Initial Zoom MapKit Swift

I'm new in Swift, and I'm trying to use MkMapView. I'd like to change the initial zoom. I searched On the API but I don't find anything.

I find only this, about zoom

mapView.zoomEnabled = true

But it isn't what I looking For.

Upvotes: 0

Views: 1798

Answers (2)

Amit Sarker
Amit Sarker

Reputation: 36

From Logan's answer, update for Swift 5+

let coordinate: CLLocationCoordinate2D = // .. populate your center
let latitudinalMeters: CLLocationDistance = 50
let longitudinalMeters: CLLocationDistance = 50
let region = MKCoordinateRegion(center: coordinate, latitudinalMeters: latitudinalMeters, longitudinalMeters: longitudinalMeters)
        self.mapView.setRegion(region, animated: false) // Set to yes to animate, you said initial load so I image this won't be visible anyways.

Upvotes: 0

Logan
Logan

Reputation: 53112

MKMaps don't have a concept of zoom as a configurable variable. Zoom enabled pertains to user interaction. You'll need to configure something manually by setting a region that encompasses your desired zoom level.

For instance, if I wanted to zoom in on some specific coordinate, I could do:

let coordinate: CLLocationCoordinate2D = // .. populate your center
let latitudinalMeters: CLLocationDistance = 50
let longitudinalMeters: CLLocationDistance = 50
let region = MKCoordinateRegionMakeWithDistance(coordinate, latitudinalMeters, longitudinalMeters)
self.mapView.setRegion(region, animated: false) // Set to yes to animate, you said initial load so I image this won't be visible anyways.

Then, adjust the latitudinal/longitudinal dimensions to meet your desired zoom level.

If you wanted, you could probably create a category that adds zoom and calls this in the background.

Upvotes: 4

Related Questions