我有一个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 { } }]; }
解决方法
更新(以反映解决方案):
拥有自己的自定义注释并实现setCoordinate和/或合成坐标可能会导致问题.
以前的方案:
[mapView removeAnnotations:[mapView.annotations]]; [mapView addAnnotations:(NSArray *)];
for (id<MKAnnotation> annotation in mapView.annotations) { [mapView removeAnnotation:annotation]; // change coordinates etc [mapView addAnnotation:annotation]; }