I’m trying to follow the overlay instructions provided here:
https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html#//apple_ref/doc/uid/TP40009497-CH6-SW15+
but the overlay isn’t showing. Here’s a step by step of what I’ve done so far:
1.) Added a new View Controller object to the storyboard
2.) Added a Map View object to the new view controller
3.) Went to File, New, New File…, UIViewController subclass, named it “MapViewController”, which added three files to my project: MapViewController.h, MapViewController.m, and MapViewController.xib
4.) Then I went to the view controller and defined its class as MapViewController
5.) Then “Control” + Clicked the MKMapView object over to the MapViewController.h which created: “@property (weak, non atomic) IBOutlet MKMapView *MKPolygonView;
6.) I then added the appropriate code to my .h and .m files to reflect the example, which I’ll include a copy of below
When I start the iOS simulator the map shows up, but the overlay doesn’t. What am I doing wrong?
MapViewController.h :
//
// MapViewController.h
// GeoShapes
//
// Created by Template User on 4/1/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController : UIViewController <MKMapViewDelegate> {
IBOutlet MKMapView *map;
}
@property (weak, nonatomic) IBOutlet MKMapView *MKPolygonView;
@end
MapViewController.m :
//
// MapViewController.m
// GeoShapes
//
// Created by Template User on 4/1/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "MapViewController.h"
@implementation MapViewController
@synthesize MKPolygonView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[self setMKPolygonView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
// Define an overlay that covers Colorado.
CLLocationCoordinate2D points[4];
points[0] = CLLocationCoordinate2DMake(41.000512, -109.050116);
points[1] = CLLocationCoordinate2DMake(41.002371, -102.052066);
points[2] = CLLocationCoordinate2DMake(36.993076, -102.041981);
points[3] = CLLocationCoordinate2DMake(36.99892, -109.045267);
MKPolygon* poly = [MKPolygon polygonWithCoordinates:points count:4];
poly.title = @"Colorado";
[map addOverlay:poly];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKPolygon class]])
{
MKPolygonView* aView = [[MKPolygonView alloc] initWithPolygon: (MKPolygon*)overlay];
aView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
aView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
aView.lineWidth = 3;
return aView;
}
return nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
Move the code bellow in
viewDidUnloadtoviewDidLoad: