1

Imagine that I have 6 circles. My timer calls first 2 circles in the beginning and overlay them on map view. Then after 2 seconds, it calls other circles and add them on map view. My problem is how to remove previous overlays.I want to see smooth transition for instance radar map.

Make it short, it would like to remove previous overlays and add the new overlays without flickering! Thanks a lot in advance!!.

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize mapView;
@synthesize timer;
@synthesize circle1,circle2,circle3,circle4,circle5,circle6;



- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    CLLocationCoordinate2D zoomLocation1;

zoomLocation1.latitude=29.830071;
zoomLocation1.longitude=-95.319099;
circle1 = [MKCircle circleWithCenterCoordinate:zoomLocation1 radius:15000];
circle1.title=@"first level";

CLLocationCoordinate2D zoomLocation2;
zoomLocation2.latitude=29.830071;
zoomLocation2.longitude=-95.319099;
circle2 = [MKCircle circleWithCenterCoordinate:zoomLocation2 radius:4000];
circle2.title=@"first level";

CLLocationCoordinate2D zoomLocation3;
zoomLocation3.latitude=29.830071;
zoomLocation3.longitude=-95.319099;
circle3 = [MKCircle circleWithCenterCoordinate:zoomLocation3 radius:6000];
circle3.title=@"second level";

CLLocationCoordinate2D zoomLocation4;
zoomLocation4.latitude=29.830071;
zoomLocation4.longitude=-95.319099;
circle4 = [MKCircle circleWithCenterCoordinate:zoomLocation4 radius:18000];
circle4.title=@"second level";

CLLocationCoordinate2D zoomLocation5;
zoomLocation5.latitude=29.830071;
zoomLocation5.longitude=-95.319099;
circle5 = [MKCircle circleWithCenterCoordinate:zoomLocation5 radius:1000];
circle5.title=@"third level";

CLLocationCoordinate2D zoomLocation6;
zoomLocation6.latitude=29.830071;
zoomLocation6.longitude=-95.319099;
circle6 = [MKCircle circleWithCenterCoordinate:zoomLocation6 radius:13000];
circle6.title=@"third level";


    MKCoordinateRegion viewRegion=MKCoordinateRegionMakeWithDistance(zoomLocation1, 60*1609, 60*1609);

MKCoordinateRegion adjustedRegion=[mapView regionThatFits:viewRegion];

[mapView setRegion:adjustedRegion animated:YES];
mapView.mapType=MKMapTypeStandard;
[mapView setDelegate:(id)self];
i=0;
}

- (void)viewDidUnload
{
[self setMapView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)drawButton:(id)sender {
timer = [NSTimer scheduledTimerWithTimeInterval:(2.0) target:self     selector:@selector(addingOverlay) userInfo:nil repeats:YES];

}


- (void)addingOverlay {

i=i+1;    
switch(i%3)
{
    case 1:
        [mapView removeOverlays: [mapView overlays]];
        [mapView addOverlay:circle1];
        [mapView addOverlay:circle2];
        break;
    case 2:
        [mapView removeOverlays: [mapView overlays]];
        [mapView addOverlay:circle3];
        [mapView addOverlay:circle4];
        break;
    case 3:
        [mapView removeOverlays: [mapView overlays]];
        [mapView addOverlay:circle5];
        [mapView addOverlay:circle6];
        break;

}


}

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay> )overlay
{
MKCircleView *circleView = [[MKCircleView alloc] initWithOverlay:overlay];
if ([overlay.title isEqualToString:@"first level"]) 
{
circleView.strokeColor = [UIColor redColor];
circleView.lineWidth = 2;
circleView.fillColor=[UIColor yellowColor];
}
else if([overlay.title isEqualToString:@"second level"])
{
circleView.strokeColor = [UIColor whiteColor];
circleView.lineWidth = 2;
circleView.fillColor=[UIColor blackColor];
}
else{
circleView.strokeColor = [UIColor greenColor];
circleView.lineWidth = 2;
circleView.fillColor=[UIColor redColor];


}

return circleView;
}




@end
casillas
  • 16,351
  • 19
  • 115
  • 215
  • 1
    Do you want the original circles to disappear, and then the new circles to appear, or do you want the original circles to **move** to new locations? – Nate Sep 23 '12 at 03:45
  • Hello Nate. I want original circles to disappear and new circles are drawn. But without flickering!! – casillas Sep 23 '12 at 03:50

1 Answers1

1

I just posted this answer to a similar question.

It has a function that I use (drawRangeRings) that draws two circles. In my example, both circles have the same center coordinate, but different radius values, and colors. You could easily change the code to use different center coordinates for each circle if you need that.

Basically, I call drawRangeRings when the center coordinate changes, and it removes the original two circles, and draws two new ones at a different location.

Let me know if this isn't exactly what you needed.

I don't see any flicker when I use this in my app.

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • Hello Nate. thanks a lot for the reply. However, that is static and valid only if you call one time,. If you call your method couple of times with timer, it would flicker. That is how exactly I implemented and same idea the one you gave as an example. – casillas Sep 23 '12 at 03:56
  • 1
    @casillas, you can call this method as many times as you like, not just one time. How fast is your timer running? It sounds like you are trying to remove and redraw the circles quickly. If that's true, then you need to tell us how quickly you're doing that. If you already have code to do this, please post the code you're using, and we can try to fix the problem. – Nate Sep 23 '12 at 08:20
  • Hello Nate, please see my code above. Thanks a lot in advance. I am thinking to use drawMapRect method and then seetNeedsDisplay method inside. What do you think? – casillas Sep 23 '12 at 23:05
  • please see my original code, I have just posted. http://stackoverflow.com/questions/12557553/mkoverlay-adding-removing-overlay – casillas Sep 24 '12 at 00:47
  • 1
    @casillas, So, I just changed my app, to use a `NSTimer` to call my `drawRangeRings` method every two seconds (like your code above). I don't see any flicker on an iPhone 4 (iOS 5). Of course, you could consider removing a circle and then drawing it again **to be flicker**, but I don't see any additional display artifacts other than the circle disappearing (as the code commands), and then appearing again. Are you running this on an old device? I guess I wouldn't recommend using `drawMapRect` (yet) without being able to reproduce the problem you're seeing. – Nate Sep 24 '12 at 06:44
  • Helo Nate, I would be very glad if you could take a look at my simple problem that I could not able find the solute yet.http://stackoverflow.com/questions/12587085/performselector-vs-progress-bar-play-stop-button – casillas Sep 25 '12 at 16:47
  • 1
    @casillas, that link you have in your last comment appears to be broken. – Nate Sep 25 '12 at 20:54
  • could u please take a look at my question that I have just posted http://stackoverflow.com/questions/12594112/remove-annotation-from-mapview-like-google-map-app – casillas Sep 26 '12 at 03:25