Monotouch - Draw MKPolyline on the map

I have MKPolyline from an array of CLLocationCoordinate2D (Points). This is all wonderful.

I added this line to the map as an overlay, for example: Map.AddOverlay(line);

I event: Map.SetVisibleMapRect(line.BoundingMapRect, true);

But the line is not displayed, although the borders of the map are correct.

I look in MKPolylineView, but cannot make it work.

Does anyone know to set the color and line width?

thank

+3
source share
2 answers

After multiple scratches, here's how to display MKPolyline on MKMapView:

Step 1: Create a Delegate Method for Map GetViewForOverlay

Map.GetViewForOverlay = Map_GetViewForOverlay;

Where Map is MKMapView.

MKOverlayView Map_GetViewForOverlay(MKMapView mapView, NSObject overlay)
{
    if(overlay.GetType() == typeof(MKPolyline))
    {
       MKPolylineView p = new MKPolylineView((MKPolyline)overlay);
       p.LineWidth = 2.0f;
       p.StrokeColor = UIColor.Green;
       return p;
    }
    else
        return null;
}

Step 2: Create a New MKPolyline Instance

MKPolyline line = MKPolyline.FromCoordinates(polyPoints);

Where polyPoints is an array of CLLocationCoordinate2D.

Step 3: add overlay to the map

Map.AddOverlay(line);

4:

Map.SetVisibleMapRect(line.BoundingMapRect, true);
+8

, MapView , , :

https://github.com/anujb/MapWithRoutes

, . obj-C, , .

,

+1

All Articles