2017-01-05 3 views
1

マップアノテーションにボタンとラベルを付けることができます。タイトル、サブタイトル、および座標の注釈は完全に機能しますが、ボタンを表示することはできません。SwiftのMKPointAnnotationにボタンを追加するには

func drawEvents(_ loc: CLLocation, title1: String) 
    { 
     mapView.delegate = self// 
     let center = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude) 
     let lat: CLLocationDegrees = center.latitude 
     let long: CLLocationDegrees = center.longitude 
     self.pointAnnotation1 = MKPointAnnotation() 
     self.pointAnnotation1.title = title1 
     self.pointAnnotation1.subtitle = "Event" 
     self.pointAnnotation1.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long) 
     self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation1, reuseIdentifier: nil) 
     self.mapView.addAnnotation(self.pinAnnotationView.annotation!) 
    } 

答えて

5

これはMKPinAnnotationViewrightCalloutAccessoryViewにUIButtonを追加することによって達成することができます。ここで は一例です:

Annotation View Button

お役に立てば幸い:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    if !(annotation is MKUserLocation) { 
     let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: String(annotation.hash)) 

     let rightButton = UIButton(type: .contactAdd) 
     rightButton.tag = annotation.hash 

     pinView.animatesDrop = true 
     pinView.canShowCallout = true 
     pinView.rightCalloutAccessoryView = rightButton 

     return pinView 
    } 
    else { 
     return nil 
    } 
} 

は、これはそのように見えるだろうものです!

+0

ありがとうございました – Steve

関連する問題