6

I need to display an MKOverlay on a map, but can't get it to actually appear.

I'm following the example from Apple's Location Awareness Programming Guide and the overlay wont display. Any help would be greatly appreciated, this is the first iPhone app I've made, so I might be missing something simple.

NavViewController.h

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

@interface NavViewController : UIViewController <MKMapViewDelegate> {
}
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

@end

NavViewController.m

#import "MSUNavViewController.h"
#import <CoreLocation/CoreLocation.h>

- (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;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 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";

    [_mapView addOverlay:poly];
}

Storyboard:

storyboard

Any coding suggestions would be greatly appreciated. Thanks!

nslocum
  • 5,037
  • 1
  • 27
  • 27

3 Answers3

7

Make sure to set mapView's delegate to the view controller instance( perhaps File's owner in this case).

In interface builder, right-click on the map view, drag from hollowed circle at the right of delegate, to the File's Owner icon at the Placeholder section in the pane on the left.

For storyboard, connect to the View Controller icon instead of File's Owner.

ZhangChn
  • 3,154
  • 21
  • 41
  • How exactly do I set the delegate to the view controller instance? I'm new to this. – nslocum Jan 29 '12 at 21:12
  • Right click on the nib's map view, first check if the MKMapView's referencing outlet is mapView - File's Owner, then check if its `delegate` outlet is `File's owner`. – ZhangChn Jan 29 '12 at 21:17
  • it looks like the delegate isn't set and I can't figure out how to set it to `File's owner'. Also, I'm on IOS 5, so the File's owner icon doesn't appear. – nslocum Jan 29 '12 at 21:40
  • 1
    Look at the picture. Drag from delegate circle to the yellow big circle at the bottom of the screen. Or add `_mapView.delegate = self;` to - (void)viewDidLoad method – Shmidt Jan 29 '12 at 21:46
  • drag from hollowed circle at the right of `delegate`, to the `File's Owner` icon at the `Placeholder` section in the pane on the left. – ZhangChn Jan 29 '12 at 21:47
  • On IOS 5, the `File's Owner` icon isn't present. To do the same thing, I added the line of code `_mapView.delegate = self;` above `[_mapView addOverlay:poly];` – nslocum Jan 29 '12 at 21:55
  • Maybe you are using storyboard. So, drag to the view controller (besides firstResponder) instead. – ZhangChn Jan 29 '12 at 22:04
0

Did you solve your problem? Just drag from the circle beside delegate (in your screen shot) to the circle which is the name of the class.. there is a setting in the inspector that would allow you to display where you are also..? You made need to centre the map in say viewDidLoad so it displays over Colorado..

jheneghan
  • 649
  • 6
  • 4
0

Probably you killing aView in - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay by return nil; Try to add else before.

- (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;
    }
    else return nil;
} 
Shmidt
  • 16,436
  • 18
  • 88
  • 136