为什么折线不显示?

Why is the Polyline not showing?

这里是我的全部代码来显示一个简单的行:

public class MapUIViewController : UIViewController
{
    MKMapView map;

    public override void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear(animated);
        CLLocationCoordinate2D loc = new CLLocationCoordinate2D(38.8895, -77.036168);
        map = new MKMapView()
        {
            MapType = MKMapType.Hybrid,
            Region = new MKCoordinateRegion(loc, new MKCoordinateSpan(0.1, 0.1)),
            Frame = View.Bounds,
        };
        View = map;
        ShowLine(loc, new CLLocationCoordinate2D(loc.Latitude + 1, loc.Longitude + 1));
    }
        
    void ShowLine(CLLocationCoordinate2D start, CLLocationCoordinate2D end)
    {
        MKGeodesicPolyline polyline = MKGeodesicPolyline.FromCoordinates(new CLLocationCoordinate2D[] { start, end });
        map.AddOverlay(polyline);
        map.OverlayRenderer = rend;
    }

    MKOverlayRenderer rend(MKMapView mapView, IMKOverlay overlay)
    {
        if (overlay is MKGeodesicPolyline line)
            return new MKPolylineRenderer(line) { LineWidth = 3, StrokeColor = UIColor.Red, FillColor = UIColor.Brown, Alpha = 1 };
        else
            return null;
    }
}

它显示指定的位置,但不显示行。

(代码在 C# 中(使用 Xamarin.ios),但在 Swift 和 Objective C 中应该类似,我的问题是关于渲染器的是 ios 功能,而不是语言功能)。

我一定是遗漏了什么,但我找不到了。

您需要在添加该行之前分配渲染器。换一种方式意味着添加线条时不存在渲染器,因此不会绘制

错误

map.AddOverlay(polyline);
map.OverlayRenderer = rend;

map.OverlayRenderer = rend;
map.AddOverlay(polyline);