【问题标题】:Multiple annotation color多种注释颜色
【发布时间】:2012-09-11 12:27:55
【问题描述】:

我有一张地图,上面有多个注释。我能够显示第一个和最后一个注释。我想给每个注释一个不同的颜色。

这是我如何插入注释的代码

if(i<1 || i >object.count-2)            
{    
        MyAnnotation* myAnnotation1=[[MyAnnotation alloc] init];                        
        myAnnotation1.coordinate=theCoordinate1;
        myAnnotation1.title=DEVNAME;
        myAnnotation1.subtitle=it.address;                        
        [mapView addAnnotation:myAnnotation1];                        
        [annotations addObject:myAnnotation1];                             
}

if 条件是读取数组的索引,只删除第一个和最后一个注解。

这是我如何在地图上放置大头针...

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
    (id <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation)  
    {

        static NSString* MyAnnotationIdentifier = @"MyAnnotationIdentifier";
        MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
                                               initWithAnnotation:annotation reuseIdentifier:MyAnnotationIdentifier] autorelease];
        customPinView.pinColor = MKPinAnnotationColorRed;
        customPinView.animatesDrop = YES;
        customPinView.canShowCallout = YES;



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

        return customPinView;
    }
    else
    {
        pinView.annotation = annotation;
    }
    return pinView;
  }

如何给不同的注解颜色?

【问题讨论】:

  • 所以你尝试了解决方案?

标签: iphone objective-c ios xcode ipad


【解决方案1】:

除了第一个和最后一个引脚之外,它当然不会工作,对于其余引脚,else 部分代码将执行。

所以MKPinAnnotationView *pinView 将是nil。所以没有分配内存的注释,不能改变颜色!!! :-)

您必须 allocinitMKPinAnnotationView *pinView 根据您的要求在某处进行其他部分。

【讨论】:

  • 你的意思是 pinview=[[MKPinAnnotationView alloc] init];我试过这个并给了我一个异常错误......
  • 这是我尝试过的... isi-2 是最后一个,否则是中间注释。这些是在 mapView 方法中编写的 if(isi-2) {pinview=[[MKPinAnnotationView alloc] init]; pinView.pinColor = MKPinAnnotationColorRed; } else { pinview=[[MKPinAnnotationView alloc] init]; pinView.pinColor = MKPinAnnotationColorGreen; }
  • 我认为你应该像这样正确分配初始化注释:MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:MyAnnotationIdentifier] autorelease]
【解决方案2】:

使用 MKAnnotationView 类并为每个引脚设置自定义图像:

MKAnnotationView *customPinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
customPinView.image = [UIImage imageNamed:@"pin1.png"];
.
.
.

您可能需要以编程方式构建 UIImage,而不是使用应用程序包中的图像,以便获得独特的颜色,这取决于您。

此外,您可以在注释上设置一个值,以帮助识别哪个注释获取哪个图像。您将继承 MKAnnotation 以添加这个名为 pinNumber 的整数属性。

myAnnotation.pinNumber = 2;

customPinView.image = [UIImage imageNamed:[NSString stringWithFormat:@"pin%d.png",annotation.pinNumber]];

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 2022-10-16
    • 1970-01-01
    • 2016-01-22
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多