This Monotouch code below will not cause an exception, but it also won’t show the polygon on the map.
var coords = new CLLocationCoordinate2D()
{
new CLLocationCoordinate2D(32.67, -81.9),
new CLLocationCoordinate2D(32.77, -81.9),
new CLLocationCoordinate2D(32.61, -81.9),
new CLLocationCoordinate2D(32.43, -81.9),
new CLLocationCoordinate2D(32.67, -81.9)
};var mkp = MKPolygon.FromCoordinates(coords);
mapView.AddOverlay(mkp);
I’ve centered the map on coords[0] and that works fine, as does a SetRegion with coords[0]. Using the coords[0], I have no problem adding an Annotation to the MapView.
I also tried this with same results (no errors, but no overlay displayed):
var mkc = MKCircle.Circle(coords[0], 100);
mapView.AddOverlay(mkc);
Thinking that perhaps the MapView wasn’t "ready" yet, I even tried a thread that waited a second, and then created the Polygon, via the InvokeOnMainThread. The map did the animated move to the coord, but still just showed the map and no overlays.
If I add this:
var mkp = new MKPolygonView(mkp);
mapView.AddOverlay(mkpv);
I get this exception:
"Objective-C exception thrown. Name: NSInvalidArgumentException Reason: – [MKPolygonView boundingMapRect] unrecognized selector sent to instance 0x7579100"
Is there perhaps a problem with my coordinate list? I’ve tried with the first coordinate at the end of the list to close the polygon and without that, with the same results.
Thanks
Adding an overlay object to the map view is not enough. You have to create a delegate object and implement the GetViewForOverlay method:
You then assign a new instance of this delegate object to your map view’s Delegate property:
Every annotation or overlay you add to a map view, needs a corresponding view.
The exception you are getting is because you are adding an MKOverlayView derived object where an MKOverlay derived is expected.