11

我有一个地图视图,其中注释的坐标不断更新,但是当我使用 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 {

        }
    }];
}
4

7 回答 7

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]; 
}
于 2013-01-03T02:30:04.173 回答
9

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

dispatch_async(dispatch_get_main_queue()) {
    self.mapView.addAnnotation(annotation)
}
于 2016-02-11T20:42:03.353 回答
8

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

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

旁注,内森的回答应该有更多的支持。我实际上需要这个帮助来删除注释,同样的问题存在并且通过将更新调度到主队列来解决。

于 2017-08-07T22:16:10.073 回答
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];

     }

 }];
}
于 2014-02-12T22:02:52.437 回答
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];
            }
        }
    }
}
于 2019-10-22T11:22:47.223 回答
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
于 2019-12-23T20:41:29.550 回答
-1

只需添加

mapView.reloadStyle(self)
于 2019-06-11T06:07:08.217 回答