MKMapView: setRegion does not work!

Ahhh ... well, calm down.

Anyone having trouble setting up the MKMapView area? He never worked with me.

This code:

-(void)setUserCenteredSpan:(MKCoordinateSpan)span{ // for this example, span = {0.5, 0.5} // Current region (just initialised) NSLog(@"%f, %f - %f, %f", self.region.center.latitude, self.region.center.longitude, self.region.span.latitudeDelta, self.region.span.longitudeDelta); // New Region MKCoordinateRegion region = MKCoordinateRegionMake([[[self userLocation] location] coordinate], span); NSLog(@"%f, %f - %f, %f", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta); // Region saved in MKMapView [self setRegion:region animated:NO]; NSLog(@"%f, %f - %f, %f", self.region.center.latitude, self.region.center.longitude, self.region.span.latitudeDelta, self.region.span.longitudeDelta); } 

Returns this log:

 30.145127, -40.078125 - 0.000000, 0.000000 0.000000, 0.000000 - 0.500000, 0.500000 0.000000, 0.000000 - 0.000000, 0.000000 

Do you know why ?!

Thank you very much, you can save me from killing yourself X (

March

EDIT: Of course, I'm on a device connected to the internet.

+4
source share
3 answers

I don’t understand exactly the previous magazines, but I know where my mistake was.

Installed MKMapView was not initialized by the frame, but with the autoresizingMask parameter set to> 0.

When the setRegion method is called, my view has not yet been created. I think that the values ​​of the region are calculated according to the view frame, so these values ​​cannot be found.

Just set the frame before doing setRegion, and it will display normally.

Until!

+12
source

Your first log uses self.region.center.latitude , but I don't see this in your MKCoordinateRegionMake call. The log tells you that no matter what you use to configure the region, lat / long is 0/0 ...

+1
source

Setting the autoresist mask helped me.

 [_mapView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; 
+1
source

All Articles