【问题标题】:MKMapView refresh after pin movespin移动后MKMapView刷新
【发布时间】:2010-11-14 11:39:45
【问题描述】:

使用新坐标更新自定义 AnnotationView。但问题是它只有在使用 MKMapView 进行一些操作后才会在视觉上更新,例如缩放或移动。 我应该如何手动更新地图上的视觉位置?

附言。我试图将区域更改为当前地图的区域。但它确实改变了缩放。很奇怪。

[mapView setRegion:[mapView region] animated:YES];

【问题讨论】:

标签: iphone objective-c android-mapview mkpinannotationview


【解决方案1】:

如果你从一个线程中添加你的注释,它不会起作用。 我遇到了同样的问题,只是用以下工作包装了添加注释的函数

[self performSelectorOnMainThread:@selector(addCameraIconOnMain:) obj waitUntilDone:true];  

-(void) addCameraIconOnMain:(myobjecttype*)obj
{
    // this isnt the entire function, customize for your own purpose.....
    [mapView addAnnotation:annotation];
}

【讨论】:

    【解决方案2】:

    我通过异步调用解决了这个错误,至少有 0.5 个延迟。

    例如:[self performSelector:@selector(redrawPins) withObject:nil afterDelay:0.5];

    其中“redrawPins”是添加和删除引脚的函数。

    【讨论】:

      【解决方案3】:

      这是 MapAnnotation 的接口:

      //  CSMapAnnotation.h
      //  mapLines
      //  Created by Craig on 5/15/09.
      //  Copyright 2009 Craig Spitzkoff. All rights reserved.
      
      #import <Foundation/Foundation.h>
      #import <MapKit/MapKit.h>    
      
      // types of annotations for which we will provide annotation views. 
      typedef enum {
          MapAnnotationTypeStart = 0,
          MapAnnotationTypeEnd   = 1,
          MapAnnotationTypeImage = 2
      } MapAnnotationType;
      
      @interface MapAnnotation : NSObject <MKAnnotation>
      {
          CLLocationCoordinate2D _coordinate;
          MapAnnotationType      _annotationType;
          NSString*              _title;
          NSString*              _subtitle;
          NSString*              _userData;
          NSString*              speed;
          NSString*              identifier;
      }
      
      @property (nonatomic, retain) NSString *speed;
      @property (nonatomic, retain) NSString *identifier;
      
      -(id) initWithCoordinate:(CLLocationCoordinate2D)coordinate 
          annotationType: (MapAnnotationType) annotationType
          title: (NSString*) title
          subtitle: (NSString*) subtitle
          speed: (NSString *) speed
          identifier: (NSString *) identifier;    
      -(id) setWithCoordinate: (CLLocationCoordinate2D) coordinate 
          annotationType: (MapAnnotationType) annotationType
          title: (NSString*) title
          subtitle: (NSString*) subtitle
          speed: (NSString*) speed
          identifier: (NSString*) identifier;    
      @property MapAnnotationType annotationType;
      @property (nonatomic, retain) NSString* userData;    
      @end    
      

      这是实现:

      //  CSMapAnnotation.m
      //  mapLines
      //  Created by Craig on 5/15/09.
      //  Copyright 2009 Craig Spitzkoff. All rights reserved.
      
      #import "MapAnnotation.h"    
      
      @implementation MapAnnotation
      
      @synthesize coordinate     = _coordinate;
      @synthesize annotationType = _annotationType;
      @synthesize userData       = _userData;
      @synthesize speed;
      @synthesize identifier;
      
      -(id) initWithCoordinate:(CLLocationCoordinate2D)coordinate 
          annotationType: (MapAnnotationType) annotationType
          title: (NSString*)title
          subtitle: (NSString*) subtitle
          speed: (NSString *) speedz
          identifier: (NSString *) identifierz
      {
          self = [super init];
          _coordinate = coordinate;
          _title  = [title retain];
          _subtitle = [subtitle retain];
          _annotationType = annotationType;
          speed = speedz;
          identifier = identifierz;
          return self;
      }    
      -(id) setWithCoordinate:(CLLocationCoordinate2D)coordinate 
          annotationType: (MapAnnotationType) annotationType
          title: (NSString*) title
          subtitle: (NSString*) subtitle
          speed: (NSString*) speedz
          identifier: (NSString*) identifierz
      {
          _coordinate = coordinate;
          _title = [title retain];
          _subtitle = [subtitle retain];
          _annotationType = annotationType;
          speed = speedz;
          identifier = identifierz;       
          return self;
      }    
      -(NSString*) title
      {
          return _title;
      }    
      -(NSString*) subtitle
      {
          return _subtitle;
      }    
      -(void) dealloc
      {
          [_title    release];
          [_userData release];
          [super dealloc];
      }    
      @end
      

      【讨论】:

        【解决方案4】:

        这里的答案不是刷新 MapView 或 Annotation!

        MKAnnotation 的坐标属性上有 KVO。如果您只是将您想要在地图上的对象的 id 指针添加到 mapview 并使用新位置更新坐标属性,则 MKMapView 将为您完成剩下的工作。

        尽可能接近免费午餐!

        【讨论】:

          【解决方案5】:

          您没有理由不能删除然后重新添加注释。这可能比移动整个地图的性能要好得多,即使这是一个假动作。

          【讨论】:

          • 有人想解释为什么这被否决了吗?这至少是一种替代方案,我认为它的性能更高。
          • 我相信大多数发现这个帖子的人都已经尝试过了;我知道我做到了。这会导致奇怪的行为。
          【解决方案6】:

          MKMapView 通过KVO 观察注解的坐标属性。您只需要遵守正确的KVO 协议,并在更新坐标之前和之后发送注释willChangeValueForKey:didChangeValueForKey:,键路径为@"coordinate"

          同样titlesubtitle 也被MKMapView 观察到。因此,如果您更新这些并希望标注中的值自动更改而无需您付出任何努力,请执行相同操作:致电 willChangeValueForKey:didChangeValueForKey:

          【讨论】:

          • 虽然 setCenterCoordinate:animated: 有效,但这里的 KVO 方式也有效,而且绝对更合适,并允许 MKMapView 更好地协调来自多个观察的更新。
          【解决方案7】:

          经过数小时的研究,我有点震惊。答案是:

          [mapView setCenterCoordinate:mapView.region.center animated:NO];
          

          不要问我为什么,但它会更新地图视图,这正是我所需要的。

          【讨论】:

          • UPD:它对 SDK 3.1 没有帮助。仍在研究中。
          猜你喜欢
          • 2022-12-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-28
          • 2010-12-14
          • 2017-01-17
          • 1970-01-01
          相关资源
          最近更新 更多