使用新坐标更新自定义AnnotationView。但问题是它只能在用MKMapView进行一些操作之后进行视觉更新,例如:缩放或移动。 如何手动更新地图上的视觉位置?
PS。我试图将区域更改为当前地图的区域。但它确实改变了变焦。这很奇怪。
[mapView setRegion:[mapView region] animated:YES];
答案 0 :(得分:19)
[mapView setCenterCoordinate:mapView.region.center animated:NO];
不要问我为什么,但它更新了mapview,这就是我所需要的。
答案 1 :(得分:12)
MKMapView
通过KVO
观察注释的坐标属性。您只需要遵守正确的KVO
协议,并在更新坐标之前和之后使用willChangeValueForKey:
的密钥路径发送注释didChangeValueForKey:
和@"coordinate"
。
title
同样也观察到subtitle
和MKMapView
。因此,如果您更新这些内容并希望自动更改标注中的值而不需要您付出任何努力,请执行相同的操作:致电willChangeValueForKey:
和didChangeValueForKey:
答案 2 :(得分: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];
}
答案 3 :(得分:2)
这里的答案不是刷新MapView或Annotation!
MKAnnotation的坐标属性上有KVO。如果您只是将地图上想要的对象的id指针添加到mapview并使用新位置更新坐标属性,MKMapView将为您完成剩下的工作。
尽可能接近免费午餐!
答案 4 :(得分:0)
我通过异步调用解决了这个错误,至少0.5延迟。
例如:[self performSelector:@selector(redrawPins) withObject:nil afterDelay:0.5];
其中“redrawPins”是添加和删除引脚的功能。
答案 5 :(得分:-2)
您没有理由不能删除然后重新添加注释。这可能比移动整个地图更有效率,即使这是一个假动作。
答案 6 :(得分:-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