2017-01-05 4 views
1

Ich möchte in der Lage sein, eine Schaltfläche und Label auf meiner Karte Annotation zu setzen. Die Annotation mit dem Titel, dem Untertitel und den Koordinaten funktioniert einwandfrei, aber ich kann keine Taste drücken.Wie man Knopf zu MKPointAnnotation in Swift hinzufügt

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!) 
    } 

Antwort

5

Dies kann durch Zugabe eines UIButton zum rightCalloutAccessoryView des MKPinAnnotationView erreicht werden. Hier ist ein Beispiel:

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

Dies ist, was das aussehen werden:

Annotation View Button

Hoffnung, das Ihnen hilft!

+0

funktioniert super danke – Steve

Verwandte Themen