22

I have added a MKCircle as MKOverlay to my MKMapView. Also I added an UISlider to decide the radius of the circle. Unfortunately when using this it seems a bit "laggy", not smootly like I want it to be.

Example: http://dl.dropbox.com/u/3077127/mkoverlayDelay.mov

This is my code:

- (void)addCircle
{
    // draw the radius circle for the marker
    double radius = 2000.0;
    MKCircle *circle = [MKCircle circleWithCenterCoordinate:location radius:radius];
    [circle setTitle:@"background"];
    [mapView addOverlay:circle];

    MKCircle *circleLine = [MKCircle circleWithCenterCoordinate:location radius:radius];
    [circleLine setTitle:@"line"];
    [mapView addOverlay:circleLine];
}

- (void)addCircleWithRadius:(double)radius
{
    MKCircle *circle = [MKCircle circleWithCenterCoordinate:location radius:radius];
    [circle setTitle:@"background"];
    [mapView addOverlay:circle];

    MKCircle *circleLine = [MKCircle circleWithCenterCoordinate:location radius:radius];
    [circleLine setTitle:@"line"];
    [mapView addOverlay:circleLine];
}

- (void)sliderChanged:(UISlider*)sender
{
    [mapView removeOverlays:[mapView overlays]];

    double radius = (sender.value * 100);

    [self addCircleWithRadius:radius];
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay{
    MKCircle *circle = overlay;
    MKCircleView *circleView = [[[MKCircleView alloc] initWithCircle:overlay] autorelease];

    if ([circle.title isEqualToString:@"background"])
    {
        circleView.fillColor = UIColorFromRGB(0x598DD3);
        circleView.alpha = 0.25;
    }
    else
    {
        circleView.strokeColor = UIColorFromRGB(0x5C8AC7);
        circleView.lineWidth = 2.0;
    }

    return circleView;
}

Does anybody have any suggestions on how I can smoothen this?

Best regards,
Paul Peelen

Paul Peelen
  • 10,073
  • 15
  • 85
  • 168
  • 1
    +1 for nicely documented question. Is there any need to remove the overlay, why not just update it? – sudo rm -rf Feb 02 '11 at 15:51
  • Thanks! I don't think there is any need.. but how can I update the overlay? – Paul Peelen Feb 02 '11 at 16:12
  • I'm not sure you can? I always thought the MapKit overlays were immutable, you can't modify them once created. This answer suggests the same: http://stackoverflow.com/questions/3171572/how-to-update-mkpolyline-mkpolylineview – lxt Feb 05 '11 at 17:18
  • Allright, thanks for the comment. Either way, I am looking for smoothening this... any ideas? – Paul Peelen Feb 05 '11 at 20:13

2 Answers2

6

I have tried your code and found a very easy way to make it smoother.

If you change the order of the calls in: - (void)sliderChanged:(UISlider*)sender

You can call [self addCircleWithRadius:radius];

before calling [mapView removeOverlays:[mapView overlays]];

Just make sure you dont remove the overlays you just added, only the old ones.

This will give you a smoother resizing, specially when the new circle is smaller than the old one.

For circles that are bigger you are probably better off using NSOperations to ensure the views are created faster, this will make it smoother.

Hope this helps.

Zebs
  • 5,378
  • 2
  • 35
  • 49
  • Good idea. I'll give it a try. Could you elaborate your suggestion with NSOperations? – Paul Peelen Feb 06 '11 at 21:20
  • Im glad you like the idea, in regards to bigger circles I would point you to the [Breadcrumb](http://developer.apple.com/library/ios/#samplecode/Breadcrumb/Introduction/Intro.html) example. – Zebs Feb 08 '11 at 21:33
  • 2
    Looking for an `NSOperation` tutorial I came by [this one](http://icodeblog.com/2010/03/04/iphone-coding-turbo-charging-your-apps-with-nsoperation/), it is very complete and should help you create the `MKCircle` objects faster. Just remember you can only update the user interface from the main run loop. – Zebs Feb 10 '11 at 18:28
  • @Zebs link broken. – Johann Burgess Nov 20 '16 at 03:26
0

Your best bet might be to draw the circle yourself, either using another UIView on top of the MKMapView, or using a MKAnnotationView within the Map View.

This blogger has done a similar thing (before iOS4 when overlays were added). http://spitzkoff.com/craig/?p=65

joerick
  • 16,078
  • 4
  • 53
  • 57