4

I have a small map on my view that I want to drop an MKCircle overlay onto. I have all the coords and radius as I am creating regions for monitoring. I would like to display this region back to the user so they will know what the boundaries are.

For the life of me, there isn't one good tutorial on the internet that will give me just the bare necessities to drop a circle onto my map and be done.

As a precursor... I have used Apple's examples with no luck. The Regions example is supposed to be the one need, but all I can get it to do is drop the pin, no circle. I even copied their classes directly into my project... no joy. So if you can either point me to a good example or layout exactly what needs to be implemented in a simple ViewController, I would be very grateful.

Bill Burgess
  • 14,054
  • 6
  • 49
  • 86

2 Answers2

14

My guess for why using the sample code didn’t work: you didn’t hook up your view controller as the map view’s delegate. First step for doing that is making sure the controller implements the MKMapViewDelegate protocol, like this (in its header file):

#import <MapKit/MapKit.h>

@interface MyViewController : UIViewController <MKMapViewDelegate>

If you’re setting up the view controller from a XIB, Ctrl-drag from the map view to your controller instance and connect it as the map view’s delegate outlet. If you’re setting it up in code, then call theMapView.delegate = self; in your -loadView or -viewDidLoad.

Then, at some point (in your -viewDidLoad, for instance),

[theMapView addOverlay:[MKCircle circleWithCenterCoordinate:someCoordinate radius:someRadius]];

…will result in the map view calling its delegate’s -mapView:viewForOverlay: method, which you can implement something like this:

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
    MKCircleView *circleView = [[MKCircleView alloc] initWithCircle:(MKCircle *)overlay];
    circleView.fillColor = [UIColor blueColor];
    return [circleView autorelease];
}
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
  • I was calling the delegate in code, but you were right, I hadn't hooked the mapView up to the delegate in my .xib. I did that and the circle dropped on the map right away. Thank you very much! – Bill Burgess Aug 26 '11 at 12:26
  • If you were already setting the delegate programmatically and it wasn’t working, it’s possible you were setting it up too early while your map view outlet was `nil`. Outlets only get set once `-loadView` is done, i.e. after the first call to `theViewController.view`; you can use them in `-viewWillAppear:`, `-viewDidLoad`, etc., but `-init` (or `-initWithNibName:bundle:` or whatever) won’t work. – Noah Witherspoon Aug 26 '11 at 16:44
  • It's -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id < MKOverlay>)overlay – Daij-Djan Mar 25 '13 at 15:37
4

It's

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <
MKOverlay>)overlay

for the full delegate method, see a complete answer for people completely lost like myself.

joce
  • 9,624
  • 19
  • 56
  • 74
henghonglee
  • 1,732
  • 3
  • 20
  • 29