Ios 在MKMapView中绘制矩形

Ios 在MKMapView中绘制矩形,ios,xamarin.ios,Ios,Xamarin.ios,我正试图在我的MKMapView中绘制一个红色矩形。我看到了地图,但没有看到矩形。我的代码: public override void ViewDidLoad() { base.ViewDidLoad(); var areaMapView = new AreaMapView(); areaMapView.SetTarget(45.5399396, -73.6534612); areaMapView.AddZone(new List<Geolocatio

我正试图在我的
MKMapView
中绘制一个红色矩形。我看到了地图,但没有看到矩形。我的代码:

public override void ViewDidLoad()
{
    base.ViewDidLoad();

    var areaMapView = new AreaMapView();

    areaMapView.SetTarget(45.5399396, -73.6534612);

    areaMapView.AddZone(new List<Geolocation>()
    {
        new Geolocation() { Latitude = 25.774, Longitude = -80.190},
        new Geolocation() { Latitude = 18.466, Longitude = -66.118},
        new Geolocation() { Latitude = 32.321, Longitude = -64.757},
        new Geolocation() { Latitude = 25.774, Longitude = -80.190},
    });

    View = areaMapView;
}

public class AreaMapView : MKMapView
{
    public AreaMapView() : base(UIScreen.MainScreen.Bounds)
    {
        this.ShowsUserLocation = true;
        this.MapType = MKMapType.Satellite;
    }

    public void SetTarget(double longitude, double latitude)
    {
        this.AddAnnotations(new MKPointAnnotation()
        {
            Title = "Target",
            Coordinate = new CLLocationCoordinate2D(longitude, latitude)
        });
    }

    public void AddZone(List<Geolocation> longitudeAndLatitudePoints)
    {
        var coords = new CLLocationCoordinate2D[longitudeAndLatitudePoints.Count];

        for (int i = 0; i < longitudeAndLatitudePoints.Count; i++)
        {
            double longitude = longitudeAndLatitudePoints[i].Longitude;
            double latitude = longitudeAndLatitudePoints[i].Latitude;

            coords[i] = new CLLocationCoordinate2D(longitude, latitude);
        }

        this.AddOverlay(MKPolyline.FromCoordinates(coords));
    }
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
var areaMapView=新建areaMapView();
areaMapView.SetTarget(45.5399396,-73.6534612);
areaMapView.AddZone(新列表()
{
新地理位置(){纬度=25.774,经度=-80.190},
新地理位置(){纬度=18.466,经度=-66.118},
新地理位置(){纬度=32.321,经度=-64.757},
新地理位置(){纬度=25.774,经度=-80.190},
});
视图=区域地图视图;
}
公共类AreaMapView:MKMapView
{
公共区域映射视图():基本(UIScreen.MainScreen.Bounds)
{
this.ShowsUserLocation=true;
this.MapType=MKMapType.Satellite;
}
公共无效设置目标(双经度、双纬度)
{
this.AddAnnotations(新的MKPointAnnotation()
{
Title=“Target”,
坐标=新的CLLocationCoordinate2D(经度、纬度)
});
}
公共无效添加区(列出纵向和纵向添加点)
{
var coords=新的CLLocationCoordinate2D[longitudeAndLatitudePoints.Count];
对于(int i=0;i
我认为应该使用MKPolygonMKPolygonRenderer添加矩形

请参阅此处:

此处示例:

遵循示例并替换代码

 mapView.OverlayRenderer = (m, o) =>
        {
            if (circleRenderer == null)
            {
                circleRenderer = new MKCircleRenderer(o as MKCircle);
                circleRenderer.FillColor = UIColor.Purple;
                circleRenderer.Alpha = 0.5f;
            }
            return circleRenderer;
        };

        circleOverlay = MKCircle.Circle(coords, 400);
        mapView.AddOverlay(circleOverlay);

这就是结果

我认为应该使用MKPolygonMKPolygon渲染器来添加矩形

请参阅此处:

此处示例:

遵循示例并替换代码

 mapView.OverlayRenderer = (m, o) =>
        {
            if (circleRenderer == null)
            {
                circleRenderer = new MKCircleRenderer(o as MKCircle);
                circleRenderer.FillColor = UIColor.Purple;
                circleRenderer.Alpha = 0.5f;
            }
            return circleRenderer;
        };

        circleOverlay = MKCircle.Circle(coords, 400);
        mapView.AddOverlay(circleOverlay);

这就是结果

我可以在swift中发布解决方案@DariusPost对于任何使用swift的人来说,它都会被他们投票支持。有一个答案我看不出来,顺便问一下,它能解决你的问题吗@DariusI还没有尝试过,今天晚些时候会尝试吗?我可以用swift发布解决方案吗@DariusPost对于任何使用swift的人来说,它都会被他们投票支持。有一个答案我看不出来,顺便问一下,它能解决你的问题吗@DariusI还没有尝试过,今天晚些时候会尝试。请为iOS添加参考。这些是xamarin的。请添加iOS的参考资料。这些是给xamarin的。