获取当前注释信息以在xamarin iOS中的视图中显示

时间:2017-02-16 18:33:26

标签: ios xamarin annotations

在我的应用程序中,我希望当用户单击我的UITextviews的注释时,可以显示有关该注释的更多描述性信息,如标题,纬度和经度。我的问题是我不知道如何在将注释添加到地图后获取此信息。以下是我用于MKAnnotationView的一些代码,它只是内置在我的自定义视图控制器中。

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

2 个答案:

答案 0 :(得分:1)

您可以按如下方式创建自定义MKAnnotation:

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

然后您可以在GetViewForAnnotation()方法中使用此注释。 有关详细信息,请查看此link

答案 1 :(得分:0)

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

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