I’m trying to get an MKMapView show my current location. I managed to get this working in an application with a single view, but now I’m trying to implement the same view inside a UITabBarController. My code is more or less the same, but I still can’t get it to work.
I’m following the BigNerdRanch guide on iPhone development. I implemented the view like they did with the other views in the book; I first wrote the init method:
#import "CurrentLocationViewController.h"
#import "MapPoint.h"
@implementation CurrentLocationViewController
- (id)init
{
self = [super initWithNibName:@"CurrentLocationViewController"
bundle:nil];
if (self) {
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:@"Location"];
UIImage *i = [UIImage imageNamed:@"Hypno.png"];
[tbi setImage:i];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[worldView setShowsUserLocation:YES];
}
return self;
}
And I made that init method the designated initializer by doing this aswell:
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
return [self init];
}
As you can see, I did call setShowsUserLocation:YES on my worldview which is declared as: IBOutlet MKMapView *worldView; in my header file.
I’m probably missing something here, but I thought that message was the only thing I needed to get the basic mapview working? Any ideas?
I can provide more code if necessary. The view does load properly btw, I checked with an obnoxious colour first and it does show the map. It just doesn’t put a blue pin on it.
Thanks.
You’ve created the MapView in a xib file, have you made sure that you have connected the worldView outlet?
Also, you don’t need to set up CoreLocation for this. Just for comparison – here’s my controller definition for the same exercise. It doesn’t show – but I set the shows user location property in the xib file rather than in code.