2017-02-16 2 views
0

In meiner Anwendung möchte ich, wenn der Benutzer auf eine Annotation für meine UITextviews klickt, um beschreibende Informationen zu dieser Annotation wie Titel, Breite und Länge anzuzeigen. Mein Problem ist, dass ich keine Ahnung habe, wie ich diese Informationen erhalten kann, nachdem die Anmerkungen zur Karte hinzugefügt wurden. Hier ist ein Teil meines Codes, den ich für den MKAnnotationView verwende, der gerade in meinen benutzerdefinierten View-Controller eingebaut ist.Abrufen aktueller Annotationsinformationen in einer Ansicht in xamarin iOS

MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation) 
    { 
     MKAnnotationView annotationView = mapView.DequeueReusableAnnotation(annotationIdentifier); 
     // Set current location and location of annotation 
     CLLocationCoordinate2D currentLocation = mapView.UserLocation.Coordinate; 
     CLLocationCoordinate2D annotationLocation = annotation.Coordinate; 

     // We don't want a special annotation for the user location 
     if (currentLocation.Latitude == annotationLocation.Latitude && currentLocation.Longitude == annotationLocation.Longitude) 
      return null; 

     if (annotationView == null) 
      annotationView = new MKPinAnnotationView(annotation, annotationIdentifier); 
     else 
      annotationView.Annotation = annotation; 

     annotationView.CanShowCallout = true; 
     (annotationView as MKPinAnnotationView).AnimatesDrop = false; // Set to true if you want to animate the pin dropping 
     (annotationView as MKPinAnnotationView).PinColor = MKPinAnnotationColor.Red; 
     annotationView.SetSelected(true, false); 

     _annotationDetailButton = UIButton.FromType(UIButtonType.DetailDisclosure); 
     _annotationDetailButton.TouchUpInside += (sender, e) => 
     { 
      //put create segue her 
      PerformSegue("ShowCollectionDetail", Self); 

     }; 

     annotationView.RightCalloutAccessoryView = _annotationDetailButton; 

     // Annotation icon may be specified like this, in case you want it. 
     // annotationView.LeftCalloutAccessoryView = new UIImageView(UIImage.FromBundle("example.png")); 
     return annotationView; 
    } 
+0

Haben Sie sich meine Antwort angesehen? –

+0

Ich tat, ich bekomme Informationen über die Anmerkungen, aber nicht die, die gerade ausgewählt ist. Ich habe diesen Code der GetViewForAnnotation-Methode hinzugefügt. SculptureTitle.Text = annotationView.Annotation.GetTitle(); Irgendwelche Vorschläge dazu, was ich noch mache, sind falsch. Es scheint, dass ich Anmerkungen nicht auf der aktuellen Anzeige bekomme. – user3020229

Antwort

1

Sie können Ihre benutzerdefinierten MKAnnotation wie folgt erstellen:

public class AnnotationModel : MKAnnotation 
{ 
    private string _title; 
    private string _subtitle; 

    public AnnotationModel(CLLocationCoordinate2D coordinate, string title, string subtitle = "") 
    { 
     this.Coords = coordinate; 
     _title = title; 
     _subtitle = subtitle; 
    } 

    public override string Title { get { return _title; } } 
    public override string Subtitle { get { return _subtitle; } } 
    public CLLocationCoordinate2D Coords; 
    public override CLLocationCoordinate2D Coordinate { get { return this.Coords; } } 
} 

Dann Sie diese Anmerkung in Ihrem GetViewForAnnotation() Methode verwenden können. Weitere Informationen finden Sie unter link.

+0

Grundsätzlich möchte ich nur Informationen über die aktuell ausgewählte Annotation anzeigen, wenn der Benutzer darauf klickt. – user3020229

+0

Hey ich habe es! Habe herausgefunden, dass ich nur DidSelectAnnotationView in der Kartenansicht aufrufen musste. Dann fügte ich mein eigenes Ereignis hinzu, wo ich map.selectedannotations anrief, die die aktuell ausgewählte Anmerkung zurückgab, dann warf ich die Infomation mit der kundenspezifischen Klasse vor, die Sie mir zeigten, danke! – user3020229

+0

map.DidSelectAnnotationView + = (object sender, MKAnnotationViewEventArgs e) => { \t \t \t \t annoList = map.SelectedAnnotations; \t \t \t \t foreach (IMKAnnotation a in annoList) \t \t \t \t { \t \t \t \t \t SculptureTitle.Text = a.GetTitle(); \t \t \t \t} \t \t \t}; – user3020229

0
map.DidSelectAnnotationView += (object sender, MKAnnotationViewEventArgs e) => 

{ annoList = map.SelectedAnnotations; foreach (IMKAnnotation a in annoList) { SculptureTitle.Text = a.GetTitle(); } };

Verwandte Themen