iOS刷新mapview上的注释

时间:2013-01-03 00:21:26

标签: ios annotations mkmapview mkannotation mkannotationview

我有一个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 {

        }
    }];
}

7 个答案:

答案 0 :(得分:18)

更新(以反映解决方案):

拥有自己的自定义注释并实现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]; 
}

答案 1 :(得分:9)

将添加注释分派给主线程,而不是尝试修改后台线程上的UI

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

答案 2 :(得分:6)

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

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

旁注,内森的回答应该有更多的赞成。我实际上需要这个帮助来删除注释,同样的问题存在并通过将更新分派给主队列来修复。

答案 3 :(得分:0)

尝试使用[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];

     }

 }];
}

答案 4 :(得分:0)

只需添加

mapView.reloadStyle(self)

答案 5 :(得分:0)

出于黑暗模式的目的,我需要刷新注释的视图。 我还必须调用委托方法来重新创建批注的视图。

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [super traitCollectionDidChange:previousTraitCollection];
    if (@available(iOS 13.0, *)) {
        if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
            for (id<MKAnnotation> annotation in self.mapView.annotations) {
                [self.mapView removeAnnotation:annotation];
                [self.mapView addAnnotation:annotation];
                [self mapView:self.mapView viewForAnnotation:annotation];
            }
        }
    }
}

答案 6 :(得分:0)

删除所有现有的注释,然后重新添加新的或更新的注释列表将刷新mapView。 首先,我尝试过:

mapView.annotations.removeAll()

但收到错误:

"Value of type '(MKMapRect) -> Set<AnyHashable>' has no member 'removeAll'"

但这可行:

for annotation in mapView.annotations{
    mapView.removeAnnotation(annotation)
}
// .. code to add the new or updated annotation list