Smooth Movement of annotation image on Mapview
Asked Answered
V

3

8

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?

Vermicular answered 13/3, 2012 at 13:48 Comment(0)
O
3

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.

Oxley answered 13/3, 2012 at 20:37 Comment(3)
i know issue lies in removing and adding annotations on map-view but is there any other way to do this?can you please tell me in details what should i do?Vermicular
Hey thanks for the reply.but its not working.its still gives me jumping like effect.Vermicular
Sorry to hear that, wish I had more experience with that call to help. Cheers and good luck.Oxley
R
1

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

Reek answered 6/4, 2012 at 7:29 Comment(0)
J
0
//SWIFT 3
UIView.beginAnimations("yourname", context: nil)
UIView.setAnimationDuration(1.0)
yourpin.coordinate = MKCoordinateForMapPoint(mycoordinate)
UIView.commitAnimations()
Justificatory answered 26/1, 2017 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.