Draw a circle of 1000m radius around users location in MKMapView

前端 未结 8 2040
死守一世寂寞
死守一世寂寞 2020-11-29 17:18

(Using iOS 5 and Xcode 4.2)

I have an MKMapView and want to draw a circle of 1000m radius around the user location.

On the surface it would seem tha

8条回答
  •  误落风尘
    2020-11-29 18:08

    I didn't understand benwad answer. So here is clearer answer:

    It's pretty easy to add a circle. Conform to MKMapViewDelegate

    @interface MyViewController : UIViewController 
    @property (weak, nonatomic) IBOutlet MKMapView *mapView;
    @end
    

    In viewDidLoad, Create a circle annotation and add it to the map:

    CLLocationCoordinate2D center = {39.0, -74.00};
    
    // Add an overlay
    MKCircle *circle = [MKCircle circleWithCenterCoordinate:center radius:150000];
    [self.mapView addOverlay:circle];
    

    Then implement mapView:viewForOverlay: to return the view.

    - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay
    {
        MKCircleView *circleView = [[MKCircleView alloc] initWithOverlay:overlay];
        [circleView setFillColor:[UIColor redColor]];
        [circleView setStrokeColor:[UIColor blackColor]];
        [circleView setAlpha:0.5f];
        return circleView;
    }
    

    But if you want the circle to always be the same size, no matter the zoom level, you'll have to do something different. Like you say, in regionDidChange:animated:, get the latitudeDelta, then create a new circle (with a radius that fits into the width), remove the old one and add the new one.

    Note from me: don't forget to connect mapview with your view controller delegate. Otherwise viewForOverlay won't be called.

提交回复
热议问题