地图套件和注释

发布于 2024-08-09 12:30:25 字数 1571 浏览 0 评论 0原文

我对 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'

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

请指教,因为我已经在这个问题上兜了一圈了两天了!!!!

提前致谢 谢丽尔

I am very new to XCode and iPhone development so please bear with me if this question is too simple. But I have a map and I have successfully added images (not pins) to it for my annotations. And I can change the image when the user selects one of the annotations.

I created a class that inherits from MKAnnotationView with the following methods:-

- (id)initWithAnnotation:

- (void)setAnnotation:

- (void)drawRect:

and I am using

- (void)touchesBegan

to know when an annotation has been selected. And in touchesBegan I am doing :-

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

to change the image. But what I am really stumped on is how do I change the image back to it's original image when the users selects the next annotation. I have tried:-

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

but it errors

and I tried

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;
}
}

}

the above code works ok until I get to ann.image = i; then it errors. The errors I get are:-

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

Yes I can see that my place object does not have an image so that's why it is going wrong. But if I create an image property on my place object - how will that change the annotations image which what I am trying to do.

Please advise as I have been going around in circles on this one for 2 days now!!!!

Thanks in advance
Cheryl

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞎闹 2024-08-16 12:30:25

谢丽尔,

我不完全遵循你想要做的事情,但这里有一些想法:

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

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

firstImage 和 secondaryImage,设置保留。

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

然后,您可以说

self.image = firstImage;

或者

self.image = secondImage.

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

您的代码:

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

不正确。它向地图请求一系列注释,然后将它们视为注释视图。

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

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

Cheryl,

I don't completely follow what you're trying to do, but here are some thoughts:

Here's what I would do to restore the original image:

In your subclass of MKAnnotationView, add two UIImage Properties,

firstImage and secondImage, set up to retain.

When you init the annotation view, set up both images. (At the point where you assign the image to your annotation view, also save it to your new firstImage property)

Then, you can say

self.image = firstImage;

or

self.image = secondImage.

That will swap the appropriate image into place, while keeping the other image around to restore.

Your code:

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

is not right. It asks the map for an array of annotations, and then treats them as annotation VIEWs.

An annotation is a data model object. It contains the data describing an annotation.

An annotation VIEW object is a temporary display object used to display an annotation on the map if it is currently visible. There are not always annotation views for every annotation on the map.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文