2017-01-05 1 views
1

Я хочу, чтобы на моей аннотации карты была помечена кнопка и ярлык. Аннотации с заголовком, субтитрами и координатами работают отлично, но я не могу получить кнопку для появления.Как добавить кнопку в MKPointAnnotation в Swift

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

Это может быть достигнуто путем добавления UIButton к rightCalloutAccessoryView из MKPinAnnotationView. Вот пример:

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

Это то, что будет выглядеть следующим образом:

Annotation View Button

Надежда, что помогает!

+0

отлично работает – Steve

Смежные вопросы