【问题标题】:mapkit select all annotationsmapkit 选择所有注释
【发布时间】:2015-08-20 09:59:58
【问题描述】:

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

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

【问题讨论】:

    标签: ios swift mapkit mapkitannotation


    【解决方案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;
    }
    

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

    【讨论】:

    • 嗨@Dev,感谢您的快速回答。我已经为标签使用了非常相似的代码。问题是如果用户点击注释,仍然会显示标准标题(&subtitle)标签,并且与自定义标签混淆。所以我想要的是: 1. 只使用标题,所有注释都应该在任何时候都能看到它们的标题,同时仍然可以“点击” 2. 使用自定义标签,但从不显示标准标签
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    相关资源
    最近更新 更多