关键代码部分
double[] lats = new double[] { 34.236995919519,34.237350707418 ,34.237989321866}; double[] lons = new double[] { 108.881978988647,108.872194290161,108.842668533325}; MKMapPoint[] pointArr=new MKMapPoint[lats.Length]; for (int i = 0; i < lats.Length; i++) { CLLocationCoordinate2D coordinate =new CLLocationCoordinate2D(lats[i],lons[i]); MKMapPoint mPoint = MKMapPoint.FromCoordinate (coordinate); pointArr [i] = mPoint; } RoteLine = MKPolyline.FromPoints (pointArr); mymapview.AddOverlay (RoteLine); mymapview.Delegate = new MyOverlayViewDelegate (this);
public MKPolyline RoteLine; public MKPolylineView RouteLineView; class MyOverlayViewDelegate:MKMapViewDelegate{ MKOverlayView overlayView = null; MapViewController controller; public MyOverlayViewDelegate(MapViewController c){ controller=c; } public override MKOverlayView GetViewForOverlay (MKMapView mapView, NSObject overlay) { if (overlay ==controller.RoteLine) { if (controller.RouteLineView == null) { controller.RouteLineView = new MKPolylineView (controller.RoteLine); controller.RouteLineView.FillColor = UIColor.Red; controller.RouteLineView.StrokeColor = UIColor.Red; controller.RouteLineView.LineWidth = 3; } overlayView = controller.RouteLineView; } return overlayView; } }