Question

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
Was it helpful?

Solution

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...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top