Search code examples
iosmkmapviewmkoverlaymkcircle

How to add circular region onto MKMapView


I am trying to add a circular region on my MKMapView like the picture below for my current location. I can pin the map with annotations but don't know how to get it to show a circular region like this with a radius and have it shaded.

enter image description here


Solution

  • So I figured it out...

    Below is how I did it along with code.

    Step 1: - Create MKCircle

    MKCircle *circleOverlay = [MKCircle circleWithCenterCoordinate:zoomLocation radius:300];//radius in meters
    

    Step 2: - Set title and add to Map Overlays

    [circleOverlay setTitle:@"Circle1"];
    
    [_mapView addOverlay:circleOverlay];
    

    Step 3: - Implement the mapView:rendererForOverlay: method in my MapViewDelegate

    -(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{
    
        if ([overlay isKindOfClass:[MKCircle class]])
        {
            MKCircleRenderer* aRenderer = [[MKCircleRenderer alloc] initWithCircle:(MKCircle *)overlay];
    
            aRenderer.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
            aRenderer.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
            aRenderer.lineWidth = 3;
            return aRenderer;
        }else{
            return nil;
        }
    }
    

    That was it! Boom! Hope it helps someone in the future! Not sure if this is the best way but it achieve my goal!

    Edit: make sure to set your mapView's delegate to self or the required delegate method will not be called.