【问题标题】:mapkit and annotationsmapkit 和注解
【发布时间】:2009-10-29 14:56:09
【问题描述】:

我对 XCode 和 iPhone 开发非常陌生,所以如果这个问题太简单,请多多包涵。但是我有一张地图,并且我已经成功地为我的注释添加了图像(不是图钉)。当用户选择其中一个注释时,我可以更改图像。

我使用以下方法创建了一个继承自 MKAnnotationView 的类:-

- (id)initWithAnnotation:

- (void)setAnnotation:

- (void)drawRect:

我正在使用

- (void)touchesBegan

知道何时选择了注释。在 touchesBegan 中我正在做:-

UIImage *i = [UIImage imageNamed:@"A.png"];
self.image = i;

更改图像。但我真正难过的是,当用户选择下一个注释时,如何将图像改回原始图像。我试过了:-

NSArray *selectedAnnotations = map.selectedAnnotations;
for(id annotationView in selectedAnnotations) {
[map deselectAnnotation:[annotationView annotation] animated:NO];
}

但它出错了

我试过了

for (MKAnnotationView *ann in map.selectedAnnotations){
if ([ann isMemberOfClass:[Place class]])
{
place = (Place *)ann;
NSLog(@"second = %@"@" %f"@" %f", place.title, place.longitude, place.latitude);
if (currentPlaceID == place.placeID) {
//UIImage *i = [UIImage imageNamed:@"A.png"];

//ann.image = i;
}
else {
UIImage *i = [UIImage imageNamed:@"pin.png"];

ann.image = i;
}
}

}

上面的代码可以正常工作,直到我到达 ann.image = i;然后它会出错。我得到的错误是:-

*** -[Place setImage:]: unrecognized selector sent to instance 0x4514370
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[Place setImage:]: unrecognized selector sent to instance 0x4514370'

是的,我可以看到我的地点对象没有图像,这就是它出错的原因。但是,如果我在我的地点对象上创建一个图像属性 - 这将如何改变我正在尝试做的注释图像。

请告知,因为我已经在这个问题上转了 2 天了!!!!

提前致谢 谢丽尔

【问题讨论】:

    标签: iphone mapkit annotations


    【解决方案1】:

    谢丽尔,

    我并不完全按照你的想法去做,但这里有一些想法:

    这是我要恢复原始图像的方法:

    在您的 MKAnnotationView 子类中,添加两个 UIImage 属性,

    firstImage和secondImage,设置retain。

    当您初始化注释视图时,设置两个图像。 (在将图像分配给注释视图时,还将其保存到新的 firstImage 属性)

    那么,你可以说

    self.image = firstImage;

    self.image = secondImage.
    

    这会将适当的图像交换到位,同时保留其他图像以进行恢复。

    您的代码:

    NSArray *selectedAnnotations = map.selectedAnnotations; for(id annotationView in selectedAnnotations) { [map    
    deselectAnnotation:[annotationView annotation] animated:NO]; }
    

    不对。它向地图请求一组注释,然后将它们视为注释视图。

    注解是一个数据模型对象。它包含描述注释的数据。

    注解 VIEW 对象是一个临时显示对象,用于在地图上显示当前可见的注解。地图上的每个注释并不总是有注释视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      相关资源
      最近更新 更多