Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need a dead simple implementation of MKCircle overlay

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.

like image 567
Bill Burgess Avatar asked Dec 02 '22 01:12

Bill Burgess


2 Answers

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];
}
like image 76
Noah Witherspoon Avatar answered Dec 04 '22 13:12

Noah Witherspoon


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.

like image 38
henghonglee Avatar answered Dec 04 '22 14:12

henghonglee