8

我已经为用户当前位置加载了自定义注释图像。我在后台每 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];
}

更新当前的纬度和经度后,我正在使用以下代码刷新地图

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

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

4

3 回答 3

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”方法称为应该用于支持拖动或频繁更新的方法。

于 2012-03-13T20:37:44.213 回答
1

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

它看起来像这样:

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

它将平滑地移动注释。

BR,马辛·舒尔克

于 2012-04-06T07:29:38.840 回答
0
//SWIFT 3
UIView.beginAnimations("yourname", context: nil)
UIView.setAnimationDuration(1.0)
yourpin.coordinate = MKCoordinateForMapPoint(mycoordinate)
UIView.commitAnimations()
于 2017-01-26T17:09:06.317 回答