0

Unless I'm wrong, a MKPolyLine is drawn on a map via the GetViewForOverlayoverride associated to the MKMapView delegate.

public override MKOverlayView GetViewForOverlay(MKMapView mapView, IMKOverlay overlay)
{}

I'd like to associate a custom property to my MKPolyLine so GetViewForOverlay could use it.

So I wrote a new class:

public class PolyLineWithPrivacy : MapKit.MKPolyline
{
    public bool PrivacyOn { get; set; }
}

I also would like to use the static FromPoint constructor.

So I have this code to construct my custom instance:

private void TracePolyLine(List<CLLocationCoordinate2D> routeParts, bool privacyon)
{
    GeoFleet.iOS.PolyLineWithPrivacy polyLine = (GeoFleet.iOS.PolyLineWithPrivacy)MapKit.MKPolyline.FromPoints(routeParts.Select(rp => new MapKit.MKMapPoint(rp.Latitude, rp.Longitude)).ToArray());
    polyLine.PrivacyOn = privacyon;

    this.mapView.AddOverlay(polyLine);
}

But this crashes (during the cast) because I can't assign a base class object to a derived class.

So what are my options here? Is there a way to construct a MKPolyLine from a collection of MKMapPoint? How can I deal with my PrivacyOn member in the renderer override?

EDIT: I'm now using the default constructor and just append points to the Points collection.

EDIT2 : Appending points does not work, the collection remains empty :(

Olivier MATROT
  • 4,515
  • 5
  • 47
  • 77
  • About making custom MKPolyline, I find one thread that you can take a look: https://stackoverflow.com/questions/26010977/how-to-make-custom-mkpolyline-in-swift-with-additional-argument-color – Cherry Bu - MSFT Jun 11 '21 at 05:48

0 Answers0