地图视图上的 iOS 刷新注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14131345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:42:35  来源:igfitidea点击:

iOS refresh annotations on mapview

iosannotationsmkmapviewmkannotationmkannotationview

提问by Jon Erickson

I have a mapview where the annotation's coordinates are constantly being updated but when I use setCoordinate, the annotation does not move. How do I refresh the annotations to reflect their coordinates?

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

        }
    }];
}

回答by Rowan Freeman

Updated (to reflect the solution):

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

Having your own custom annotations and implementing setCoordinate and/or synthesizing coordinate may cause issues.

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

Source: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html

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

Previous solution:

以前的解决方案:

You can simply remove all of the annotations and then re-add them.

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

[mapView removeAnnotations:[mapView.annotations]];

[mapView addAnnotations:(NSArray *)];

or remove them and re-add them one by one:

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

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

回答by Nathan

Dispatch the add annotation to the main thread rather then attempt to modify the UI on a background thread

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

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

回答by biomiker

Swift 3.0version of Nathan's answer (thank you Nathan):

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

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

Side note, Nathan's answer should have far more upvotes. I actually needed this help with removing an annotation, the same issue exists and is fixed by dispatching the update to the main queue.

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

回答by oscar castellon

Try using [self.mapView removeAnnotations:self.mapView.annotations]:

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

     }

 }];
}

回答by Marek Manduch

For dark mode purposes I needed to refresh the views of annotations. I had to call also the delegate method to recreate the view of the annotations.

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

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

回答by Claytog

Removing all existing annotations, and re-adding the new or updated annotation list will refresh the mapView. Firstly I tried:

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

mapView.annotations.removeAll()

but received the error:

但收到错误:

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

However this worked:

但是这有效:

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

回答by apollovishwas

Just Add

只需添加

mapView.reloadStyle(self)