多个注释颜色 [英] Multiple annotation color
问题描述
我有一个带有多个注释的地图。我能够显示第一个和最后一个注释。我想给每个注释一个不同的颜色。
i have a map with multiple annotation on it. i was able to show the first and the last annotations. i want to give each annotation a different color.
这里是我的如何插入我的注释的代码
here is my code of how to insert my annotations
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条件是读取数组的索引只删除第一个和最后一个注释。
the if condition is reading the index of the array to only drop the first and last annotation.
这里是我如何放置地图上的引脚...
and here is how do i drop the pins on the map...
-(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;
}
如何使用不同的注释颜色?
how to different annotations color?
推荐答案
使用MKAnnotationView类并为每个引脚设置自定义图像:
Use MKAnnotationView class and set a custom image for each pin:
MKAnnotationView *customPinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
customPinView.image = [UIImage imageNamed:@"pin1.png"];
.
.
.
而不是使用应用包中的图像,您可能需要以编程方式构建您的UIImage,以便您可以实现独特的颜色,这取决于你。
instead of using images from your app bundle, you might need to programmatically build your UIImage so you can achieve unique colors, this is up to you.
此外,您可以在注释上设置一个值,以帮助确定哪个注释获取哪个图像。你将子类化MKAnnotation来添加这个名为pinNumber的整数属性。
Furthermore, you can set a value on your annotation to help identify which annotation gets which image. You'll subclass MKAnnotation to add this integer property called pinNumber.
myAnnotation.pinNumber = 2;
myAnnotation.pinNumber = 2;
customPinView.image = [UIImage imageNamed:[NSString stringWithFormat:@"pin%d.png",annotation.pinNumber]];
这篇关于多个注释颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!