4

Aaaarg... ok, let's calm myself.

Did someone have any problem with setting the region of a MKMapView ? It 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 that 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 ??!

Thanks a lot, you can save me from killing myself X(

Mart

EDIT : Of course, I am on the device, connected to internet.

Martin
  • 11,881
  • 6
  • 64
  • 110

3 Answers3

12

I don't understand exactly the previous logs, but I know where was my error.

The instancied MKMapView was not initialised with a frame, but with an autoresizingMask set to > 0.

When the setRegion method was called, my view was not framed yet. I think the region values are calculated according to the view frame, so these values couldn't be found.

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

Bye !

Martin
  • 11,881
  • 6
  • 64
  • 110
  • Indeed. I was setting the region of a mapView in the controller's `viewDidLoad`, but alas the frame size was (0,0). Setting the region has no effect until the view's frame is established (apparently). The frame should be set by the time `viewDidAppear` is invoked. – wcochran Mar 21 '13 at 17:11
  • thank you for this, you just saved another night of hair pulling. Interestingly, I was following a Ray Wenderlich tutorial which said that viewWillAppear will work, but did not for me. Not sure why, but viewDidAppear did the trick! – gdbj Jun 13 '13 at 06:15
  • That is beautiful! +1 – Eduardo Jun 08 '16 at 12:58
1

Your first log uses self.region.center.latitude, but I don't see that anywhere in your call to MKCoordinateRegionMake. The log is telling you that whatever you are using to make the region to set, has a lat/long of 0/0...

Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
  • [[[self userLocation] location] coordinate] should return the lat/long of the user position. MKCoordinateRegionMake's first arg should set the lat/long of the user position, be it remains 0/0... – Martin Sep 16 '09 at 08:22
1

Setting the AutoResizing Mask helped me.

[_mapView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];   
Adam Cooper
  • 867
  • 1
  • 6
  • 23