1

我正在使用 swift 和 MapKit 构建一个应用程序。它有几个自定义点注释。默认情况下,我想随时在所有注释上方显示标题。因此他们都会被选中。因此,我可以从这篇文章中获得灵感并使用:

[mapView selectAnnotation:pinView animated:YES]

但是我仍然需要能够通过单击它来选择一个图钉并根据此注释获取一个新的 mapScope。我怎么能做到?

如果第一个解决方案不可能,我会在每个注释上使用一个特定的标签。但是有没有办法永远隐藏注释标题?

编辑:

这是自定义点注释的代码:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    println("viewForAnnotation")
    if !(annotation is MKPointAnnotation) {
        return nil
    }
    var seleccion:Bool

    let cpa = annotation as! CustomPointAnnotation

    let reuseId = cpa.nickName as String
    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)


    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView.canShowCallout = true
//            create and add UILabel only when actually creating MKAnnotationView

        var nameLbl: UILabel! = UILabel(frame: CGRectMake(-24, 40, 100, 30))
        nameLbl.tag = 42
        nameLbl.textColor = UIColor.blackColor()
        nameLbl.font = UIFont(name: "Atari Classic Extrasmooth", size: 10)
        nameLbl.textAlignment = NSTextAlignment.Center
        anView.addSubview(nameLbl)
    }
    else {
        anView.annotation = annotation
    }

    anView.image = cpa.image
    if cpa.toBeTriggered == true {
        anView.selected = true
    }

    if let nameLbl = anView.viewWithTag(42) as? UILabel {
        nameLbl.text = cpa.nickName
    }

    return anView
}
4

1 回答 1

1

当您使用自定义注释视图时,此代码将对您有所帮助。

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation  //thammu
{
    static NSString *reuseId = @"StandardPin";

    MKAnnotationView *aView;

    //MKAnnotationView *aView = (MKAnnotationView *)[Mapsview dequeueReusableAnnotationViewWithIdentifier:reuseId];

    aView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                     reuseIdentifier:reuseId];

    aView.enabled = YES;
    aView.canShowCallout = YES;

     aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];


    UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(-41,-22,121,25)];
    img.image=[UIImage imageNamed:@"annotation-bg@2x.png"]; //This is the Details image view


    UILabel *lblPlaceholder1=[[UILabel alloc]init];

    lblPlaceholder1 = [[UILabel alloc]initWithFrame:CGRectMake(4,2,117, 21)];
    lblPlaceholder1.backgroundColor=[UIColor clearColor];
    lblPlaceholder1.numberOfLines=1;
    [lblPlaceholder1 setTextColor:[UIColor redColor]];
    lblPlaceholder1.textAlignment=NSTextAlignmentCenter;
    lblPlaceholder1.text=annotation.title;
    [lblPlaceholder1 setTextColor:[UIColor darkGrayColor]];
    [lblPlaceholder1 setFont:[UIFont fontWithName:@"Roboto-Regular" size:12.0f]];


    NSString *subTitleStr = annotation.subtitle;


    [img addSubview:lblPlaceholder1];

    [aView addSubview:img];

    [img setUserInteractionEnabled:YES];

    aView.image = [UIImage imageNamed:@"tracker.png"];//This is marker image view

    aView.annotation = annotation;

    return aView;
}

这里地图中的所有注释默认都是可见的。

于 2015-08-20T10:12:06.000 回答