MKMapView set region
Asked Answered
S

2

6

I'm attempting to use the MKMapView. I've succeeded in making the world map appear. However, I can't seem to change the region:

I have a button that will perform this:

NSLog(@"%f, %f, %f, %f,
mapView.region.center.latitude,
mapView.region.center.longitude,
mapView.region.span.latitudeDelta,
mapView.region.span.longitudeDelta);

Now, in my viewDidLoad method, I attempt to set a starting region to view:

CLLocationCoordinate2D startCoord;
startCoord.latitude = 49.0;
startCoord.longitude = -123.0;
[mapView setRegion:MKCoordinateRegionMakeWithDistance(startCoord, 200, 200) animated:YES];

When the view does load, it shows the same world map instead of a smaller area as expected. Immediately logging the region's attributes gives me:

0.000000, 0.001417, 0.000000, 0.000000

Moving the map around a bit, resizing and zooming doesn't change these values (except that the second one goes back to 0.000000).

It would appear to be that mapView.region does not correspond to what I'm seeing on the screen, but I'm quite certain I made the IB links correctly, I'm looking at them right now. What could be the problem?

Sausage answered 18/8, 2011 at 5:6 Comment(1)
[mapView regionThatFits:MKCoordinateRegionMakeWithDistance(startCoord, 200, 200)]; Add this line to your code.Sexagenary
E
25

You need to do

CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(49, -123);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:MKCoordinateRegionMakeWithDistance(startCoord, 200, 200)];
[mapView setRegion:adjustedRegion animated:YES];
Econometrics answered 18/8, 2011 at 6:42 Comment(0)
A
0

For Swift code

let coordinate = CLLocationCoordinate2D(latitude: 49, longitude: -123)
let region = self.mapView.regionThatFits(MKCoordinateRegion(center: coordinate, latitudinalMeters: 200, longitudinalMeters: 200))
self.mapView.setRegion(region, animated: true)
Asinine answered 18/6, 2020 at 8:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.