Utkarsh Sharma
Utkarsh Sharma

Reputation: 61

Change MKMapView's Zoom - Swift 4

I using Apple's MapKit and MKMapView to show a location on screen. The function I am using is:

func displayLocation() {
    locationMap.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2DMake(userConnected.siteConnectedLat!, userConnected.siteConnectedLong!), span: MKCoordinateSpanMake(0.05, 0.05)), animated: true)

    let locationPin = CLLocationCoordinate2D(latitude: userConnected.siteConnectedLat!, longitude: userConnected.siteConnectedLong!)
    let annotation = MKPointAnnotation()
    annotation.coordinate = locationPin

    locationMap.addAnnotation(annotation)
    locationMap.showAnnotations([annotation], animated: true)
}

Is is then called in the viewDidLoad method of the View Controller. Apple's documentation says that the span should change the zoom but even changing the span is putting minimum effect on the map. I want it to be zoomed out enough so that we can clearly see like 3-4 European countries, i.e, a significant amount of zoom-out.

This is what my map looks like in simulator: This is what my map looks like in simulator

This is how it looks like after I press enlarge: This is how it looks like after I press enlarge

Upvotes: 0

Views: 693

Answers (1)

Sweeper
Sweeper

Reputation: 270698

You need to set the region of the map in viewDidAppear, not viewDidLoad. When viewDidLoad is called, the map view has just been loaded - the map hasn't been rendered yet, so you can't set its region.

Another thing to change is the span. (0.05, 0.05) seems too small a span to show 3-4 countries. You should try something bigger, like (5, 5). Remember that these numbers represent in degrees the width and height of the map region.

Upvotes: 2

Related Questions