2015-05-29 67 views
4

我正在为mapview实现自定义标注视图。我得到这个输出MKMapView的Swift自定义标注

enter image description here

对于注解视图我使用

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 

    let reuseId = "mapPin" 
    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? CustomCalloutView 
    if anView == nil { 
    anView=CustomCalloutView(annotation: annotation,reuseIdentifier:reuseId) 
     anView!.image = UIImage(named:"mappin.png") 
     let button : UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton 
     button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside) 
     anView!.rightCalloutAccessoryView=button 
    //anView!.addSubview(viewC) 
    mapViewControl.addAnnotation(annotation) 
    } 
    else { 
     anView!.annotation = annotation 
    } 

    return anView 
} 

,并弹出标注视图

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!){ 
    var viewC:UIView=UIView(frame: CGRectMake(0, 0, 50, 50)) 
    viewC.backgroundColor = UIColor.blackColor() 

    view.addSubview(viewC) 
    viewC.center = CGPointMake(viewC.bounds.size.width*0.1, -viewC.bounds.size.height*0.5) 


} 
+0

为什么有downvote? – Poonam

+2

我没有downvote,但也许这是因为你没有说明问题是什么。顺便说一下:在viewForAnnotation中,你为什么要调用addAnnotation - 这没有任何意义。另外,使用calloutAccessoryControlTapped委托方法代替rightCalloutAccessoryView的addTarget。 – Anna

+1

下一次更直接与您的问题。但这实际上是一个很好的实现:) – Michael

回答

1

我得到了解决。它的变化非常小。在viewForAnnotation隐藏基本标注如下,

anView!.canShowCallout = false