Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smooth Movement of annotation image on Mapview

I have loaded the custom annotation image for user current location.I am updating the current user location after every 1 sec in background.

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self performSelectorOnMainThread:@selector(tempupdate) withObject:nil waitUntilDone:NO];
[pool release];

-(void)tempupdate
{
    NSLog(@"callToLocationManager");
    mylocationManager = [[CLLocationManager alloc]init];
    NSLog(@"locationManagerM = %@",mylocationManager);
    mylocationManager.delegate = self;
    mylocationManager.desiredAccuracy = kCLLocationAccuracyBest;
    mylocationManager.distanceFilter = 500;
    [mylocationManager startUpdatingLocation];
}

After updating the current latutude and longitude i am refreshing the map using following code

MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.002;
span.longitudeDelta=0.002;
CLLocationCoordinate2D location;
location.latitude=[lat doubleValue];
location.longitude=[longt doubleValue];
region.span=span;
region.center=location;
addAnnotation=[[AddressAnnotation alloc]initWithCoordinate:location];
addAnnotation.mTitle=@"You are here";
[self.mapView removeAnnotations:self.mapView.annotations];
[self.mapView addAnnotation:addAnnotation];
[self.mapView setRegion:region animated:TRUE];
[self.mapView regionThatFits:region];

But every time custom annotation image blinks before adding to the map.How to avoid this flicker effect?

like image 596
Akshay Aher Avatar asked Mar 13 '12 13:03

Akshay Aher


2 Answers

I haven't tested this yet, but based on a quick glance at your code, I would guess the issue lies in your removal of the annotation and then adding a new one.

Have you tried just editing the already attached annotation's properties?

NSArray* curAnnotations = [mapView annotations];
AddressAnnotation* aa = [curAnnotations lastObject]; // I am assuming you only have 1 annotation
aa.mTitle = @"You are here"; // or don't do this if it never changes
[aa setCoordinate:location];

Note: Apple Docs specifically call out the 'setCoordinate' method as something that should be used to support dragging or frequent updates.

like image 102
MobileVet Avatar answered Oct 24 '22 02:10

MobileVet


you mustn't remove annotation from map. Instead change the annotation coordinate in the UIView beginAnimations-commitAnimations block;

It will look something like this:

[UIView beginAnimations:@"yourAnimationName" context:nil];
[UIView setAnimationDuration:1.0];
[yourAnnotation setCoordinate:yourNewCoordinate];
[UIView commitAnimations];

It will move smoothly the annotation.

BR, Marcin Szulc

like image 44
Marcin Szulc Avatar answered Oct 24 '22 04:10

Marcin Szulc