Mapview上注释图像的平滑移动

时间:2012-03-13 13:48:12

标签: iphone ios4 mkmapview mkpinannotationview mkmapviewdelegate

我已为用户当前位置加载了自定义注释图像。我在后台每1秒后更新当前用户位置。

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];
}

更新当前latutude和经度后,我使用以下代码刷新地图

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];

但每次自定义注释图像在添加到地图之前都会闪烁。如何避免这种闪烁效应?

3 个答案:

答案 0 :(得分:3)

我还没有对此进行测试,但基于对代码的快速浏览,我猜测问题在于删除注释然后添加新注释。

您是否尝试过编辑已经附加的注释的属性?

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];

注意: Apple Docs专门将“setCoordinate”方法称为应该用于支持拖动或频繁更新的方法。

答案 1 :(得分:1)

您不得从地图中删除注释。而是更改UIView beginAnimations-commitAnimations块中的注释坐标;

看起来像这样:

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

它会平滑地移动注释。

BR, Marcin Szulc

答案 2 :(得分:0)

//SWIFT 3
UIView.beginAnimations("yourname", context: nil)
UIView.setAnimationDuration(1.0)
yourpin.coordinate = MKCoordinateForMapPoint(mycoordinate)
UIView.commitAnimations()