gpt4 book ai didi

iphone - MKMapView:显示用户当前位置时,引脚颜色会发生变化

转载 作者:行者123 更新时间:2023-11-28 20:30:18 25 4
gpt4 key购买 nike

我有针对不同位置的自定义图钉图片,我可以同时显示所有不同的图钉。但问题是,当我显示用户的当前位置时,所有 Pin 图的颜色都会改变。

代码如下:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;

if ([annotation isKindOfClass:[MapAnnotation class]]) {

MKAnnotationView *test=[[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:@"AnnotationIdentifier"];

test.canShowCallout = YES;
// test.animatesDrop = YES;


UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
test
.rightCalloutAccessoryView = rightButton;

switch (_pushpinTag) {
case 5:
test.image = [UIImage imageNamed:@"pushpin_green.png"];
break;

case 6:
test.image = [UIImage imageNamed:@"pushpin_blue.png"];
break;

case 7:
test.image = [UIImage imageNamed:@"pushpin_black.png"];
break;

case 8:
test.image = [UIImage imageNamed:@"pushpin_yellow.png"];
break;

case 3:
test.image = [UIImage imageNamed:@"pushpin_red.png"];
break;

default:
break;
}

return test;
}
}

现在在不同的按钮按下时,会显示不同的 Pin 图(带有自定义图像)。假设我有绿色、蓝色、黑色和黄色的别针。我按下按钮以显示绿色图钉,然后是蓝色图钉,然后是黑色图钉,所有图钉都显示在各自的图像中。但是当我按下按钮以显示用户当前位置时,所有 Pin 图都变为上次按下的 Pin,即黑色。

这是显示用户当前位置的代码:

- (IBAction)currentLocationButton:(id)sender {

_mapView.showsUserLocation = YES;
[_mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];

}

谁能指出我做错了什么?

谢谢大家:)

最佳答案

您的注释需要包含一些您可以用来在 viewForAnnotation 中设置其颜色的内容。 MKAnnotation 协议(protocol)将所有注释定义为具有标题、副标题和坐标。如果标题和副标题不足以确定您想要图钉的颜色,请编写您自己的类并添加一个名为 pinType 的属性。然后,当您根据用户按下的按钮创建注释集 pinType 时。当 viewForAnnotation 被调用时,您执行通常的 dequeueReusableAnnotationViewWithIdentifier/initWithAnnotation 以准备好 View ,将提供的注释转换到您的类并使用其 pinType 设置图像。这里有一些未经测试的代码,足以给你灵感

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MyAnnotation class]])
{
MyAnnotation* myAnno = (MyAnnotation)annotation;
MKAnnotationView *test;

test = (MKAnnotationView *)[_mapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationIdentifier"];
if (view == nil)
{
test=[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"AnnotationIdentifier"];
}
switch(myAnno.pinType)
{
case kBLACK_TAG: test.image = [UIImage imageNamed:@"pushpin_black.png"];
break;
}
}
}

关于iphone - MKMapView:显示用户当前位置时,引脚颜色会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12409636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com