iOS刷新mapview上的注释

我有一个mapview,其注释的坐标不断更新,但是当我使用setCoordinate时,注释不会移动。 如何刷新注释以反映其坐标?

- (void)updateUnits { PFQuery *query = [PFQuery queryWithClassName:@"devices"]; [query whereKeyExists:@"location"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { for (PFObject *row in objects) { PFGeoPoint *geoPoint = [row objectForKey:@"location"]; CLLocationCoordinate2D coord = { geoPoint.latitude, geoPoint.longitude }; for (id<MKAnnotation> ann in mapView.annotations) { if ([ann.title isEqualToString:[row objectForKey:@"deviceid"]]) { [ann setCoordinate:coord]; NSLog(@"latitude is: %f" , coord.latitude); NSLog(@"Is called"); break; } else { //[self.mapView removeAnnotation:ann]; } } } } else { } }]; } 

更新(以反映解决scheme):

拥有自己的自定义注释并实现setCoordinate和/或合成坐标可能会导致问题。

资料来源: http : //developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html

先前的解决

您可以简单地删除所有的注释,然后重新添加它们。

 [mapView removeAnnotations:[mapView.annotations]]; [mapView addAnnotations:(NSArray *)]; 

或删除它们并重新添加它们:

 for (id<MKAnnotation> annotation in mapView.annotations) { [mapView removeAnnotation:annotation]; // change coordinates etc [mapView addAnnotation:annotation]; } 

向主线程发送添加注释,而不是尝试在后台线程上修改UI

 dispatch_async(dispatch_get_main_queue()) { self.mapView.addAnnotation(annotation) } 

Swift 3.0版纳森的回答(谢谢Nathan):

 DispatchQueue.main.async { mapView.addAnnotation(annotation) } 

注意,Nathan的答案应该有更多upvotes。 实际上我需要这个帮助来移除一个注解,同样的问题存在,并通过调度更新到主队列来解决。

尝试使用[self.mapView removeAnnotations:self.mapView.annotations]

 - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self.mapView removeAnnotations:self.mapView.annotations]; PFQuery *query = [PFQuery queryWithClassName:@"caches"]; [query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) { for (PFObject *comment in comments) { NSString *tit = comment[@"titulo"]; NSString *lat = comment[@"latitud"]; NSString *lon = comment[@"longitud"]; float latFloat = [lat floatValue]; float lonFloat = [lon floatValue]; CLLocationCoordinate2D Pin; Pin.latitude = latFloat; Pin.longitude = lonFloat; MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc]init]; annotationPoint.coordinate = Pin; annotationPoint.title = tit; [self.mapView addAnnotation:annotationPoint]; } }]; }