0

Trying to learn my way around MKMapView, but I've followed several tutorials and I can't seem to get it to zoom into my defined location, instead it just loads to a whole map view centered around the Atlantic ocean. I'm guessing something has changed in iOS6 that means the tutorials I am using no longer work? (The tutorials are iOS3-iOS5).

I've made a ViewController in Interface Builder, given it the custom class of MapViewController. I then dragged on MKMapView and using the Assistant Editor put this into MapViewController.h

MapViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapViewController : UIViewController
@property (weak, nonatomic) IBOutlet MKMapView *myMapView;

@end

MapViewController.m

#import "MapViewController.h"

#define CLS_LATITUDE 54.85477
#define CLS_LONGITUDE -1.57371
#define SPAN 0.10f;

@interface MapViewController ()

@end

@implementation MapViewController
@synthesize myMapView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

 - (void)viewDidLoad
{
    [super viewDidLoad];
    MKCoordinateRegion myRegion;
    CLLocationCoordinate2D center;
    center.latitude = CLS_LATITUDE;
    center.longitude = CLS_LONGITUDE;
    MKCoordinateSpan mySpan;
    mySpan.latitudeDelta = SPAN;
    mySpan.longitudeDelta = SPAN;
    myRegion.center = center;
    myRegion.span = mySpan;
    [myMapView setRegion:myRegion animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 @end
William Robinson
  • 1,038
  • 17
  • 32

1 Answers1

1

The code for the region looks correct. Are you sure you have linked up the myMapView outlet in interface builder?

enter image description here

If you click and hold on the "File's Owner" object on the left, then hold Ctrl and drag from the File's Owner to the map view in the interface, when you let go of the mouse button you should get the option to set the outlet to myMapView. You can then see the link in the Connections inspector as shown on the right.

EDIT:

OK, so it appears that, as you are using Autolayout, the map view's frame has not been set by the time viewDidLoad gets called. I suggest you move it to - (void)viewWillLayoutSubviews. For example:

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self setLocation];
}

-(void)setLocation {
    // Set region etc...
}
Ian L
  • 5,553
  • 1
  • 22
  • 37
  • I think so, that's what @property (weak, nonatomic) IBOutlet MKMapView *myMapView; is doing in the .h file right? – William Robinson Nov 14 '12 at 21:05
  • No, that just declares the property as an outlet. You actually have to link them in Interface Builder. I'll make an edit to my answer. – Ian L Nov 14 '12 at 21:26
  • It was already like that, my MapViewController has the Outlet myMapView -> Map View, and the MKMapView itself has the Referencing Outlet myMapView -> Map View Controller. – William Robinson Nov 14 '12 at 21:49
  • I've just copied your code into a test project and the map centres correctly around Chester-le-Street. I suggest adding an NSLog(@"%@", myMapView) to the viewDidLoad method to check that it's not null. – Ian L Nov 15 '12 at 09:48
  • I tried it today and it worked the first time. Now it just shows the whole globe. This is confusing and doesn't seem right. Here is what the NSLOG says when it doesn't work, 2012-11-15 14:06:11.670 Restaurant[623:c07] – William Robinson Nov 15 '12 at 14:06
  • This is all very strange! That log is showing a frame with no size. Are you using Autolayout for your nib? The autoresize property seems very strange. I'm also confused that when you get a map with no size how you are seeing any map at all. What else do you have in the nib? – Ian L Nov 15 '12 at 14:32
  • Nothing out the ordinary. Using Storyboard, I've got my main xib, tapping Map goes to MapViewController using the modal method, and on that view controller is just a full sized Map. Autosize is checked just because that's the default, but unchecking it changes nothing. I just deleted the view controller, added a new one, made the class MapViewController, added a new Map, linked that as the myMapView Referencing Outlet and Annotations still work, Location is still Cupertino, but I'm zoomed out to the world. – William Robinson Nov 15 '12 at 15:10
  • When I use this on my phone it works? Maybe it's an issue with the simulator? – William Robinson Nov 15 '12 at 15:33
  • Nope, now it's giving me the world too. – William Robinson Nov 15 '12 at 15:39
  • Sorry William, but I'm really not sure where to go from here without laying my hands on the code. The fact that you are seeing it work as expected sometimes, and not the rest of the time is very confusing! – Ian L Nov 15 '12 at 16:09
  • It seems if you turn Autolayout off it works correctly. I have no idea why this would be the case though! – Ian L Nov 16 '12 at 08:52