9

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

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

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

7 回答 7

19

经过几个小时的研究,我有点震惊。答案很简单:

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

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

于 2009-07-30T08:37:00.850 回答
13

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

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

于 2010-07-30T13:26:45.230 回答
9

如果您从线程中添加注释,它将无法正常工作。我遇到了同样的问题,只是用以下方法包装了添加注释的函数

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

-(void) addCameraIconOnMain:(myobjecttype*)obj
{
    // this isnt the entire function, customize for your own purpose.....
    [mapView addAnnotation:annotation];
}
于 2010-11-19T01:12:47.487 回答
2

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

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

尽可能接近免费午餐!

于 2010-09-07T20:44:21.410 回答
0

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

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

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

于 2011-08-01T19:21:35.477 回答
-2

您没有理由不能删除然后重新添加注释。这可能比移动整个地图更有效,即使它是一个假动作。

于 2010-07-30T13:29:29.837 回答
-7

这是 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
于 2009-10-10T13:56:37.767 回答