我有一个应用程序,我在其中显示一张带有一个注释的小 map ,该 map 位于我创建的 objectDetailScreen 中,每当设置新对象时都会更新此屏幕,同时还会显示新 map 。 map 作为对象的注释。
每当我尝试移动 View 、点击注释图钉或缩放时, map 都会卡住一段时间,我不知道为什么会这样做。该应用程序仅适用于 iPad(iPad 2、iOS 4.3.5)。这是设置 map 的代码:
- (void) setObject:(AchmeaObject *)_object
{
if(kaart != nil)
{
[kaart removeFromSuperview];
kaart = nil;
}
kaart = [[MKMapView alloc] initWithFrame:CGRectMake(340, 380, 400,300)];
[kaart setDelegate: self];
[kaart setUserInteractionEnabled:YES];
CLLocationCoordinate2D coordinate;
coordinate.latitude = [_object.latitude doubleValue];
coordinate.longitude = [_object.longitude doubleValue];
double miles = 2;
double scalingFactor = ABS( cos(2 * M_PI * coordinate.latitude /360.0) );
MKCoordinateSpan span;
span.latitudeDelta = miles/69.0;
span.longitudeDelta = miles/(scalingFactor*69.0);
MKCoordinateRegion region;
region.span = span;
region.center = coordinate;
[kaart setRegion: region animated:YES];
ObjectAnnotation *sa = [[ObjectAnnotation alloc] initWithName: _object.plaats Address: _object.adres Coordinate:coordinate];
NSArray *anotations = [NSArray arrayWithObject: sa];
[kaart addAnnotations:anotations];
[self.view addSubview:kaart];
}
我不知道为什么会发生这种情况,但是当它第一次显示时,它需要几秒钟来响应任何用户交互,并且每次交互后它至少需要几秒钟,直到几次完全卡住后。
对象注释.m
#import "ObjectAnnotation.h"
@implementation ObjectAnnotation
@synthesize coordinate = _coordinate;
- (id) initWithName: (NSString *) _name Address: (NSString *) _address Coordinate: (CLLocationCoordinate2D) _coord{
self = [super init];
name = [_name retain];
address = [_address retain];
_coordinate = _coord;
return self;
}
- (NSString *)title {
return name;
}
- (NSString *)subtitle {
return address;
}
- (void)dealloc
{
[name release];
name = nil;
[address release];
address = nil;
[super dealloc];
}
@end
最佳答案
我怀疑这与重新添加新的 map View 有关。在 xib 中或在 View Controller 生命周期的早期添加 map View 并将其隐藏,然后显示它并更新其位置(使用 setRegion:animated:
)。每次创建新的 map View 既昂贵又不必要;如果您这样做是因为想要摆脱某些状态(注释、 map 区域),请了解如何在现有 map View 中重置该状态。
它也可能与您未提供源代码的 ObjectAnnotation 有关。
关于ios - MKMapView 在用户交互时卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7384627/