【问题标题】:iOS refresh annotations on mapviewiOS 在 mapview 上刷新注释
【发布时间】:2012-12-17 08:51:07
【问题描述】:

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

        }
    }];
}

【问题讨论】:

  • 这可以通过更改 Annotation 对象中的坐标来完成。它不需要删除注释,然后添加到地图以使更改生效
  • @NakulSudhakar 但它没有改变?

标签: ios annotations mkmapview mkannotation mkannotationview


【解决方案1】:

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

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

【讨论】:

  • 当您可以更改它们的坐标时,这会更加密集。
  • 它们是自定义注释吗?如果是这样,您是否重载了坐标属性?
  • 是的,它是定制的。重载坐标属性是什么意思?
  • 您是否实现了 setCoordinate 方法或将@synthesize 坐标放在任何地方?您不太可能有,但这会导致问题。
  • 其实,如果你看上面,我使用 id 作为 ann 指针的类。所以它不会是一个自定义注释吗?但是当第一次在另一个方法中添加注解时,它是一个自定义注解,我在其中声明了 setCoordinate 方法和合成坐标。
【解决方案2】:

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

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

【讨论】:

  • 谢谢!这帮助我解决了我的问题,除非我手动移动地图,否则 MapView 不会更新我添加的注释。
【解决方案3】:

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

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

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

【讨论】:

  • 这是答案,直到 Apple 有更好的解决方案!
【解决方案4】:

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

     }

 }];
}

【讨论】:

    【解决方案5】:

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

    - (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】:

      删除所有现有的注解,并重新添加新的或更新的注解列表将刷新 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
      

      【讨论】:

        【解决方案7】:

        只需添加

        mapView.reloadStyle(self)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-01-22
          • 1970-01-01
          • 1970-01-01
          • 2011-09-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多