Map Challenge, setRegion no effect


I’ve been busy with the Map Challenge for the view controllers.
I figured I wouldn’t be using the code I previously made but start from scratch as I’d probably learn more.
I made a UIViewController named MapViewController which has the following code:

//  MapViewController.h
//  HypnoTime

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

@interface MapViewController : UIViewController <MKMapViewDelegate>
    IBOutlet MKMapView *globalView;



//  MapViewController.m
//  HypnoTime

#import "MapViewController.h"

@implementation MapViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UITabBarItem *tbi = [self tabBarItem];
        [tbi setTitle:@"Map"];
    return self;

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)u
    NSLog(@"Mapview is updating user location: %@", u.location);
    NSLog(@"%@", globalView);
    CLLocationCoordinate2D loc = [u coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [globalView setRegion:region];

#pragma mark - View lifecycle

- (void)viewDidLoad
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    globalView = [[MKMapView alloc] init];
    [globalView setShowsUserLocation:YES];
    [globalView setDelegate:self];
    NSLog(@"globalView is loaded");

- (void)viewDidUnload
    [super viewDidUnload];
    [globalView release];
    globalView = nil;
    NSLog(@"globalView is nil");

- (void)dealloc
    [globalView release];
    [super dealloc];


In the didUpdateUserLocation it nicely logs:
"Mapview is updating user location: <+37.33169300, -122.03045700> +/- 100.11m (speed -1.00 mps / course -1.00) @ 10/8/11 12:11:40 AM Central European Summer Time"
Yet the map does not set its region afterwards.

I double checked the connections in the .xib file, the Map View is connected to File’s Owners delegate and the IBOutlet globalView of the File’s Owner is connected to the Map View.
I was under the impression that the whole locationManager wasn’t necessary and the log output shows that as well.
I’m wondering what is wrong with my thinking or code because I don’t get why the Map View doesn’t update.

(With the previous assignments I did have to use the location hack but as this location actually has coordinates I figured that would not be needed, forcing them to 0, 0 didn’t help either so)

Thanks for the help, can’t stand bugs in my code! :wink:


You are creating another MKMapView and assigning globalView to point at it in viewDidLoad. In viewDidLoad, remove the line of code that creates a new MKMapView (it already points at the one from the XIB file). You also don’t need to set the delegate because it has been set in the XIB file.


This was really useful to me - many thanks. I was trying to be too clever, and your simple code showed me my mistakes!