我有一个必须推送到map的表视图。我正在尝试从注释中获取标题以显示在地图视图中,我需要比较字幕值的值以根据注释值精确地显示在地图中,但我正在获取地图是标题和字幕值。我不想显示字幕,但我希望它比较值 这是我的代码:
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
MKAnnotationView *pinView = nil;
if(annotation != mapview.userLocation)
{
NSLog(@"%i",k);
static NSString *defaultPinID = @"defaultPinID";
pinView = (MKAnnotationView *)[mapview dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
NSString *st=@"0";
NSString *str = [annotation subtitle];
if ([str isEqualToString:st]) {
//pinView.pinColor = MKPinAnnotationColorGreen;
pinView.image=[UIImage imageNamed:@"greyDot.png"];
//return pinView;
}
NSString *st1=@"10";
if ([str isEqualToString:st1]) {
//pinView.pinColor = MKPinAnnotationColorGreen;
pinView.image=[UIImage imageNamed:@"blackDot.png"];
//return pinView;
}
NSString *st2=@"20";
if ([str isEqualToString:st2]) {
//pinView.pinColor = MKPinAnnotationColorPurple;
pinView.image=[UIImage imageNamed:@"greendot.png"];
//return pinView;
}
NSString *st3=@"30";
if ([str isEqualToString:st3]) {
//pinView.pinColor = MKPinAnnotationColorRed;
pinView.image=[UIImage imageNamed:@"blueDot.png"];
//return pinView;
}
pinView.canShowCallout = YES;
infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
//Display *t=annotation;
infoButton.tag=k;
[infoButton addTarget:self action:@selector(pinLabelClicked:) forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = infoButton;
}
else {
[mapview.userLocation setTitle:@"I am here"];
}
return pinView;
//k++;
}
这是我用于生成注释的代码
- (void)generateAnnotations
{
[mapview setMapType:MKMapTypeStandard];
[mapview setZoomEnabled:YES];
[mapview setScrollEnabled:YES];
[mapview setDelegate:self];
for( int i=0;i<[addressArray count];i++ ){
AddressInfo *address = [addressArray objectAtIndex:i];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [address.latitude floatValue];
region.center.longitude = [address.longitude floatValue];
region.span.latitudeDelta=10.01f;
region.span.longitudeDelta=10.01f;
[mapview setRegion:region animated:YES];
//[mapview setRegion:region animated:TRUE];
//[mapview regionThatFits:region];
DisplayMap *ann = [[DisplayMap alloc] init];
//ann.tt=i;
k=i;
ann.title = address.name;
ann.subtitle = [NSString stringWithFormat:@"%@",address.category];
ann.sa = address.streetaddress;
ann.sb = address.suburb;
ann.ph = address.phoneNo;
ann.index = i;
//ann.image = [NSString stringWithFormat@"image_%d.png",i];
ann.coordinate=region.center;
[mapview addAnnotation:ann];
[ann release];
}
}
我在这里做的是我正在根据这个值比较类别值,我需要在地图视图中以相对颜色显示图钉。有人知道如何做到这一点吗?
如上一个问题所述:
如何使注释字幕从标题菜单中消失?
您需要将subtitle
留空,并在注释类中使用单独的自定义属性来存储类别。
这样,subtitle
将不会显示在标注中(因为它将为空白),但您仍然可以获取注释的类别以做出决策。
首先,在您的注释类 .h(即 DisplayMap.h)中,为 category
添加一个属性:
@property (nonatomic, copy) NSString *category;
接下来,在 Display.m 中,合成属性:
@synthesize category;
接下来,在 generateAnnotations
中,您当前设置了注释的subtitle
,改为设置category
并将subtitle
留空(不设置):
ann.category = [NSString stringWithFormat:@"%@",address.category];
最后,在viewForAnnotation
中,可以检查注解的category
。 为此,请首先检查批注是否为 DisplayMap
类型,然后强制转换它。
所以替换这部分:
NSString *str = [annotation subtitle];
//code that sets pinView.image based on value of str...
有了这个:
pinView.image = set_to_some_default_image;
if ([annotation isKindOfClass:[DisplayMap class]])
{
DisplayMap *dmAnn = (DisplayMap *)annotation;
NSString *str = dmAnn.category;
//code that sets pinView.image based on value of str...
}
一个完全独立的问题是,您似乎正在使用按钮标签和自定义方法来处理标注按钮按下。 在自定义方法中,您可能正在尝试获取点击的批注的数据。 我强烈建议不要使用按钮标签或自定义方法。 有关更好的替代方法,请参阅以下内容:
- 如何防止与MKAnnotation关联的数据在弹出标注和用户点击披露按钮后丢失?
- 详细披露后的注释细节?