如何在地图视图中仅显示pin(注释)标题? [英] how to show only pin(annotation) title in map view?

查看:109
本文介绍了如何在地图视图中仅显示pin(注释)标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个必须推入地图的表格视图。我试图从注释中获取标题以在地图视图中显示,我需要比较字幕值的值以在地图中完全基于注释值显示,但我在地图中获得的是标题和副标题值。
我不想显示副标题,但我希望它比较价值
这是我的代码:

I have a Table view that has to push into map. I am trying to get the title from the annotation to show in the map view and I need to compare the value from subtitle value to show in map exactly based on annotation value, but I am getting in map is title and subtitle value. I don't want to show the subtitle but i want it to compare the values Here is my code:

-(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];
}
}

我在这里做的是我在比较类别值根据这个值我需要在地图视图中显示相对颜色的引脚..
任何人都知道如何做到这一点?

what I am doing here is I am comparing category value based on this value I need to show pins in map view with relative color.. any one have idea how to do this?

推荐答案

如前一个问题中所述:

如何让标题字幕从标题菜单中消失?

你需要将字幕留空并在注释类中使用单独的自定义属性来存储您的类别。

you need to leave subtitle blank and use a separate custom property in your annotation class to store your category.

这样,字幕将不会显示在标注中(因为它将为空白),但您仍然可以获取注释的类别以进行决策。

That way, the subtitle will not display in the callout (since it will be blank) but you will still be able to get the annotation's category to make decisions.



首先,在你的注释类.h(也就是DisplayMap.h)中,为类添加一个属性

@property (nonatomic, copy) NSString *category;

接下来,在Display.m中,合成属性:

Next, in Display.m, synthesize the property:

@synthesize category;

接下来,在 generateAnnotations 中,您当前所在的位置设置注释的字幕,设置类别保留字幕空白(不设置)

Next, in generateAnnotations, where you currently set the annotation's subtitle, set the category instead and leave subtitle blank (don't set it):

ann.category = [NSString stringWithFormat:@"%@",address.category];



最后,在 viewForAnnotation ,您可以检查注释的类别。为此,首先检查注释是否为 DisplayMap 类型然后再进行投射。


Finally, in viewForAnnotation, you can check the annotation's category. To do so, first check if the annotation is of type DisplayMap and then cast it.

因此请更换此部分:

NSString *str = [annotation subtitle];
//code that sets pinView.image based on value of str...

with这个:

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...
}



一个完全独立的问题是看起来你正在使用按钮标签和自定义方法来处理按下标注按钮。在自定义方法中,您可能正在尝试获取已点击的注释的数据。我强烈建议不要使用按钮标签或自定义方法。有关更好的替代方案,请参阅以下内容:


A completely separate issue is that it looks like you're using a button tag and a custom method to handle the callout button press. In the custom method, you may be trying to get the data of the annotation that was tapped. I very strongly do not recommend using a button tag or a custom method. For much better alternatives, see the following:

  • How to keep data associated with MKAnnotation from being lost after a callout pops up and user taps disclosure button?
  • Annotation details after detail disclosure pressed?

这篇关于如何在地图视图中仅显示pin(注释)标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆