gpt4 book ai didi

ios - 为 iOS 动态设置 map 图钉颜色

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:12:51 24 4
gpt4 key购买 nike

我解析了一个包含字符串 0、1 和 2 的 xml。

//供引用0 = 绿色1 = 红色2 = 紫色

我有一个确认 MKAnnotaion 的类,它包含以下属性变量。

CLLocationCoordinate2D  coordinate;
NSString *title;
NSString *subtitle;
MKPinAnnotationColor pinColor;

这个类被命名为 MyAnnotation

现在,在 map View 的 viewDidLoad 中,我运行一个 for 循环来遍历已解析的数据如下所示(locationArray 保存此数据,我提取所有信息就好了。

 for (int i = 0; i < locationArray.count; i++) {
myAnnotation =[[MyAnnotation alloc] init];

NSString *thePointName = [[locationArray objectAtIndex:i]xmlName];
NSString *theAddress = [[locationArray objectAtIndex:i]xmlAddress];

NSString *latString = [[locationArray objectAtIndex:i]xmlLat];
NSString *lonString = [[locationArray objectAtIndex:i]xmlLon];

//这是拉出表示引脚颜色的0、1或2个字符串的字符串 poinType保留为字符串

    pointType = [[locationArray objectAtIndex:i]xmlType];

double theLatitude = [latString doubleValue];
double theLongtitude = [lonString doubleValue];

userLocation.latitude=theLatitude;
userLocation.longitude=theLongtitude;

myAnnotation.coordinate=userLocation;
myAnnotation.title=[NSString stringWithFormat:@"%@", thePointName];
myAnnotation.subtitle=[NSString stringWithFormat:@"%@", theAddress];

//I log that the points are actually giving either of the colors and if so set MyAnnotation class to the pincolor

NSLog(@"Points Color %@", pointType);
if ([pointType isEqualToString:@"0"]){
myAnnotation.pinColor = MKPinAnnotationColorGreen;
}else if ([pointType isEqualToString:@"1"]){
myAnnotation.pinColor = MKPinAnnotationColorRed;
}else if ([pointType isEqualToString:@"2"]) {
myAnnotation.pinColor = MKPinAnnotationColorPurple;
}

[mapView addAnnotation:myAnnotation];

}

现在在 MKAnnotationView View 中我执行以下操作

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

{

// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;

// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;

//set pin color to the correct colour
if (myAnnotation.pinColor = MKPinAnnotationColorGreen) {
pinView.pinColor = MKPinAnnotationColorGreen;
}

else if (myAnnotation.pinColor = MKPinAnnotationColorRed) {
pinView.pinColor = MKPinAnnotationColorRed;

}

else if (myAnnotation.pinColor = MKPinAnnotationColorPurple){
pinView.pinColor = MKPinAnnotationColorPurple;
}



UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:@selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;


UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Profile.png"]];
pinView.leftCalloutAccessoryView = profileIconView;

return pinView;
}

我也试过上面的方法,但是它没有设置引脚颜色。其他都很好!

    //set pin color to the correct colour
if (pointType isEqualToString:@"0") {
pinView.pinColor = MKPinAnnotationColorGreen;
}

else if (pointType isEqualToString:@"1") {
pinView.pinColor = MKPinAnnotationColorRed;

}

else if (pointType isEqualToString:@"2"){
pinView.pinColor = MKPinAnnotationColorPurple;
}

最佳答案

此代码在 viewForAnnotation 中:

if (myAnnotation.pinColor = MKPinAnnotationColorGreen) {

将无法工作有两个原因:

  1. 它使用单个等号用于赋值——而不是用于检查相等性。它需要使用两个等号来检查是否相等。但是,这并没有解决主要问题,即原因 #2...

  2. 代码正在检查 myAnnotation 的值这是此委托(delegate)方法之外的变量集。 不保证委托(delegate)方法将与 for 循环同步调用,其中 myAnnotation已设置。不要假设 viewForAnnotation将在您调用 addAnnotation 后立即调用.如果 map View 需要在缩放或平移后再次显示注释,甚至可以针对同一注释多次调用委托(delegate)方法。

相反,您必须使用 annotation传递给委托(delegate)方法的参数。这是对 map View 想要在委托(delegate)方法的当前调用中查看的注释的引用。

annotation参数一般输入为 id<MKAnnotation> ,您首先必须将其转换为您的自定义类,然后您可以访问任何自定义属性:

//first make sure this annotation is our custom class before casting it...
if ([annotation isKindOfClass:[MyAnnotation class]])
{
MyAnnotation *currentAnn = (MyAnnotation *)annotation;

if (currentAnn.pinColor == MKPinAnnotationColorGreen) {
pinView.pinColor = MKPinAnnotationColorGreen;
}

else if (currentAnn.pinColor == MKPinAnnotationColorRed) {
pinView.pinColor = MKPinAnnotationColorRed;

}

else if (currentAnn.pinColor == MKPinAnnotationColorPurple) {
pinView.pinColor = MKPinAnnotationColorPurple;
}
}

或者更简单:

//first make sure this annotation is our custom class before casting it...
if ([annotation isKindOfClass:[MyAnnotation class]])
{
MyAnnotation *currentAnn = (MyAnnotation *)annotation;
pinView.pinColor = currentAnn.pinColor;
}

(不相关,但为什么代码设置了 rightButton 的标题,即使它不可见?)

关于ios - 为 iOS 动态设置 map 图钉颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13522198/

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