简体   繁体   中英

Xamarin iOS - Mapkit ArgumentNullException: Value cannot be null

My question is similar to this but I am not sure how my code relates to the answers given in the other post.

I am using MapKit in Xamarin iOS to create a custom map for my Xamarin iOS project. I have a few different custom things happening at the moment, and am using Polygons annotations and now circles that are added to my map.

I have just started implementing adding MKCircle to my map, but when I try to add Circle Overlays to my map I am receiving this error:

System.ArgumentNullException: Value cannot be null. Parameter name: polygon

I think it is being I trying to return the same overlay to two renderers, but I am not sure how to ammend this. Here is my code:

for(int i=0; i < hazards.Count; i++) //This adds 3 circles in my example
{
    LatLong ltlng = JsonConvert.DeserializeObject<LatLong>(hazards[i].coordinates);
    coords[i].Latitude = Convert.ToDouble(ltlng.latitude);
    coords[i].Longitude = Convert.ToDouble(ltlng.longitude);
    var overlay = MKCircle.Circle(coords[i], Convert.ToDouble(hazards[i].radius));
    nativeMap.AddOverlay(overlay); //this is the suspected problem
}

And my renderer code here:

    MKOverlayRenderer GetOverlayRenderer(MKMapView mapView, IMKOverlay overlayWrapper)
    {
        if (!Equals(overlayWrapper, null))
        {
            var overlay = ObjCRuntime.Runtime.GetNSObject(overlayWrapper.Handle) as IMKOverlay;
            polygonRenderer = new MKPolygonRenderer(overlay as MKPolygon)
            {
                FillColor = UIColor.Red,
                StrokeColor = UIColor.Blue,
                Alpha = 0.4f,
                LineWidth = 9
            };
        }
        return polygonRenderer;
    }

Do I need to add something to my renderer code like this?:

circleRenderer = new MKCircleRenderer(overlay as MKCircle){};

It appears all your overlays are MKCircle based:

var overlay = MKCircle.Circle(coords[i]

In your GetOverlayRenderer you casting all overlays received as MKPolygon objects which will result in a null object.

polygonRenderer = new MKPolygonRenderer(overlay as MKPolygon)

You are then trying to create a MKPolygonRenderer render for each of your overlays which would not work if you actually did have an MKCircle-based overlay.

If all your overlays are MKCircle based, then yes use:

new MKCircleRenderer(overlay as MKCircle){};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM