如何在 Xamarin.Forms.Maps 中自定义 MkMapView Callout/View

How to customize a MkMapView Callout/View in Xamarin.Forms.Maps

我想在 iOS 中完全自定义一个标注,以便在左侧显示一张图片和 3 行文本,但是我似乎无法显示这些行,无论如何它只显示一行我已经尝试过,这是我获取自定义标注的代码:

CustomMapRenderer.cs

protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
    {
        MKAnnotationView annotationView = null;

        if (annotation is MKUserLocation)
            return null;

        var customPin = GetCustomPin(annotation as MKPointAnnotation);
        if (customPin == null)
        {
            return null;
        }

        annotationView = mapView.DequeueReusableAnnotation(customPin.Name);
        if (annotationView == null)
        {
            annotationView = new CustomMKAnnotationView(annotation, customPin.Name);
            annotationView.Image = UIImage.FromFile("icon.png");
            annotationView.CalloutOffset = new CGPoint(0, 0);
            annotationView.LeftCalloutAccessoryView = new UIImageView(UIImage.FromFile("icon.png"));
            annotationView.RightCalloutAccessoryView = new UIImageView(UIImage.FromFile("icon_heart_red_24.png"));
            ((CustomMKAnnotationView)annotationView).Name = customPin.Name;
            ((CustomMKAnnotationView)annotationView).Url = customPin.Url;
            ((CustomMKAnnotationView)annotationView).Description = customPin.PlaceDescription;
            annotationView.CanShowCallout = true;
        }
        else
        {
            annotationView.Annotation = annotation;
        }

        configureDetailView(annotationView);

        return annotationView;
    }


void configureDetailView(MKAnnotationView annotationView)
    {
        int width = 100;
        int height = 50;

        var snapshotView = new UIView();
        snapshotView.TranslatesAutoresizingMaskIntoConstraints = false;
        NSDictionary views = NSDictionary.FromObjectAndKey(snapshotView, new NSString("snapshotView"));
        snapshotView.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:[snapshotView(200)]", new NSLayoutFormatOptions(), null, views));
        snapshotView.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:[snapshotView(50)]", new NSLayoutFormatOptions(), null, views));

        var options = new MKMapSnapshotOptions();
        options.Size = new CGSize(width, height);
        options.MapType = MKMapType.SatelliteFlyover;
        options.Camera = MKMapCamera.CameraLookingAtCenterCoordinate(annotationView.Annotation.Coordinate, 250, 65, 0);

        var snapshotter = new MKMapSnapshotter(options);
        snapshotter.Start((snapshot, error) =>
        {
            if (snapshot != null)
            {
                UILabel label = new UILabel();
                UILabel label2 = new UILabel();
                UILabel label3 = new UILabel();
                label.Text = ((CustomMKAnnotationView)annotationView).Description;
                label2.Text = ((CustomMKAnnotationView)annotationView).Description2;
                label3.Text = ((CustomMKAnnotationView)annotationView).Description3;
                label.Frame = new CGRect(0, 0, 200, 50);
                // Add your custom controls here
                snapshotView.AddSubviews(label, label2, label3);
            }
        });

        annotationView.DetailCalloutAccessoryView = snapshotView;
    }

我在网上找到了这段代码,但我不知道如何像 snapshotter.Start() 方法中所说的那样添加我的自定义控件。

你能帮我让它像这样工作吗:

//   -------------------------
//  |  ______   Title         |
//  | |      | text           |
//  | |Image | text     Button|
//  | |      | text           |
//  |  ------                 |
//   -------------------------
//

非常感谢您的帮助

尝试在 configureDetailView 方法中添加代码。

void configureDetailView(MKAnnotationView annotationView)
        {
            int width = 100;
            int height = 50;

            var snapshotView = new UIView();
            snapshotView.TranslatesAutoresizingMaskIntoConstraints = false;
            NSDictionary views = NSDictionary.FromObjectAndKey(snapshotView, new NSString("snapshotView"));
            snapshotView.AddConstraints(NSLayoutConstraint.FromVisualFormat("H:[snapshotView(200)]", new NSLayoutFormatOptions(), null, views));
            snapshotView.AddConstraints(NSLayoutConstraint.FromVisualFormat("V:[snapshotView(50)]", new NSLayoutFormatOptions(), null, views));

            var options = new MKMapSnapshotOptions();
            options.Size = new CGSize(width, height);
            options.MapType = MKMapType.SatelliteFlyover;
            options.Camera = MKMapCamera.CameraLookingAtCenterCoordinate(annotationView.Annotation.Coordinate, 250, 65, 0);

            var snapshotter = new MKMapSnapshotter(options);
            snapshotter.Start((snapshot, error) =>
            {
                if (snapshot != null)
                {
                    UILabel label = new UILabel();
                    UILabel label2 = new UILabel();
                    UILabel label3 = new UILabel();
                    label.Text = ((CustomMKAnnotationView)annotationView).Description1;
                    label2.Text = ((CustomMKAnnotationView)annotationView).Description2;
                    label3.Text = ((CustomMKAnnotationView)annotationView).Description3;
                    UIButton uIButton = new UIButton(UIButtonType.System);
                    uIButton.SetTitle("hello", UIControlState.Normal);
                    
                    uIButton.Frame = new CGRect(150, 10,100,15);
                    label.Frame = new CGRect(50, 0, 100, 15);
                    label2.Frame = new CGRect(50, 10, 100, 15);
                    label3.Frame = new CGRect(50, 20, 100, 15);
                    // Add your custom controls here
                    snapshotView.AddSubviews(label, label2, label3,uIButton);
                }
            });

            annotationView.DetailCalloutAccessoryView = snapshotView;
        }