I have written a simple method to utilize MKMapViewDelegate and I am having some trouble displaying the user location in the xcode 5 simulator. My understanding is that the code should be executed after a button from a previous view controller is pressed and the corresponding UIViewController class is created. However when the button is pressed and the View Controller with said map view is pushed onto the stack it simply displays the map with no zoom and no location. If I run the same code as a single view stand alone app it works just fine. What must I add or change to get the map to zoom to the users current location (in simulator its the San Fran headquarters) Here is the code I am using;
header:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface SPMenuViewController : UIViewController <MKMapViewDelegate>
@property (strong, nonatomic) IBOutlet MKMapView *myMapView;
@end
implementation:
#import "SPMenuViewController.h"
@interface SPMenuViewController ()
@end
@implementation SPMenuViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.myMapView.delegate = self;
[self.myMapView setShowsUserLocation:YES];
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
// Coords
CLLocationCoordinate2D myLocation = [userLocation coordinate];
// Zoom Region
MKCoordinateRegion zoomRegion = MKCoordinateRegionMakeWithDistance(myLocation, 2500, 2500);
// Show Location
[self.myMapView setRegion:zoomRegion animated:YES];
}
I think it should be noted that the previous view is Embedded in a Navigation Controller and a UIButton Class is connected by "push" segue to the View Controller containing the map view. Please help thank you!
If you set a breakpoint, is the userLocation value valid?
Setting showsUserLocation:YES should handle getting/displaying userLocation for you, but doesn't guarantee that the location will be on the map. Since you're setting the user location and zoomRegion in a delegate method, check when/if that method is actually called before the view appears. Could be it doesn't have an initial value to work with.
You could try setting the initial userLocation and zoomRegion in viewDidLoad instead of in didUpdateLocation to see if that works any better. This all looks like it should work ok.
Maybe a missing XCode setting to simulate location? Current Location in the Simulator