1

I'm trying to learn how to use MKMapView and have created a sample application to do so; however, in my code I'm making a call to setRegion:animated to change the center point and the span, but the map never updates. I've run through a few threads on StackOverflow (SO) where others have mentioned similar problems and tried implementing their solutions to no avail.

Things I made sure to try in my code based on other's SO threads I found:

  • Ensure I initialized the MKMapView with a frame
  • Tried running the setRegion:animated method call on the main thread

My included my code below, can anyone shed some light on why my MKMapView instance will not update it's view?

EDIT: Correct code shown below: MK_ViewController.h:

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

@interface MV_ViewController : UIViewController {
    IBOutlet MKMapView *myMapView;
    MKCoordinateRegion defaultRegion;
    CLLocationCoordinate2D defaultCenter;
    MKCoordinateSpan defaultSpan;
}

@end

MK_ViewController.m:

#import "MV_ViewController.h"

@interface MV_ViewController ()

@end

@implementation MV_ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // initialize default map view properties
    defaultCenter = CLLocationCoordinate2DMake(33.732894,-118.091718);
    defaultSpan = MKCoordinateSpanMake(0.028270, 0.0465364);

    // Setup MapView's inital region
    defaultRegion = MKCoordinateRegionMake(defaultCenter, defaultSpan);

    // create the map view
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    myMapView = [[MKMapView alloc] initWithFrame:screenRect];

}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    // update map's initial view
    [myMapView setRegion:defaultRegion animated:YES];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

MK_ViewController+MKMapViewDelegate.m:

#import "MV_ViewController.h"

@implementation MV_ViewController (MKMapViewDelegate)

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    MKCoordinateRegion region = mapView.region;
    CLLocationCoordinate2D center = region.center;
    MKCoordinateSpan span = region.span;

    NSLog(@"Center (lat,lon): (%f,%f)",center.latitude,center.longitude);
    NSLog(@"Span (lat,lon): (%f,%f)",span.latitudeDelta,span.longitudeDelta);
}

@end
Anil
  • 2,539
  • 6
  • 33
  • 42
  • 1
    Can you see anything on the screen or is it just white? Is the view loaded from a xib file? I only ask because I can't see if you're adding the map view to the main view with `[self.view addSubview:myMapView]` – Jason Cabot Oct 07 '12 at 20:35
  • I do see the map on my view, with the entire U.S. visible in the viewport. The map never updates using the coordinates I provided. But I do think you are on to something, let me try adding the MapView to the SubView. – Anil Oct 07 '12 at 20:37
  • So I updated some code, and I think you were on to something. In my NIB file, I had added an MKViewMap object, but hadn't linked it up to anything in my application. So instead what I did was removed it from the NIB, added the `[self.view addSubView:myMapView]` to my code and it fired up! Thanks for pointing me in the right direction. – Anil Oct 07 '12 at 20:48
  • Completely unrelated but in viewDidAppear, it's calling `[super viewDidDisappear:animated];` (instead of `[super viewDidAppear:animated];`). –  Oct 07 '12 at 20:51
  • 1
    Great, glad it helped. If you're using a nib file, you might as well add the MKMapView in the nib file, hooking it up to an outlet, then you don't have to manually create the map view or set it's frame meaning you can delete the following lines: `CGRect screenRect = [[UIScreen mainScreen] bounds]; myMapView = [[MKMapView alloc] initWithFrame:screenRect];` – Jason Cabot Oct 07 '12 at 20:52
  • @Jason: Thanks, I went back to see what was the reason for the map to not update after I added the referencing outlet to my NIB, it was because I was declaring a frame in my code, but the NIB already created a frame so it was conflicting. I've wired everything back up in the NIB so that I could eliminate the unnecessary code and it's working well now. Thanks. – Anil Oct 07 '12 at 20:54
  • @JasonCabot: I would like to close out this question and mark it as answered. if you could be kind enough to add an answer with your suggestions about the code `[self.view addSubview:myMapView]` in your first comment and the rewiring of my outlets in the NIB that would be great. That way I can give credit where it is deserved. Cheers. – Anil Jan 13 '14 at 18:25

1 Answers1

0

I solved the issue by removing the MKViewMap object from the NIB, and instead added it using the following code in the MK_ViewController.m file:

[self.view addSubView:myMapView];

The issue was that the NIB wasn't properly wired up to the view controller, and the view controller was creating it's own map view.

Thank you @Jason Cabot for pointing me in the right direction.

Anil
  • 2,539
  • 6
  • 33
  • 42