2017-01-31 5 views
0

Я хочу нарисовать линию маршрута, используя mkMap в xamarin ios. мой код работает правильно, но он не показывает линию маршрута между точками. мой код приведен ниже моя первая картина показывает точку начальных аннотаций и вторая картина показывает конечной точку аннотациилиния маршрута не отображается с использованием MkMap в xamarin ios

MapView Код:

private MKMapView _map; 
     private MapDelegate _mapDelegate; 

    public QiblaCompassVC (IntPtr handle) : base (handle) 
    { 
    } 
    public override void ViewDidLoad() 
    { 
     base.ViewDidLoad(); 
     _map = new MKMapView(mapsView.Bounds) 
     { 
      MapType = MKMapType.Standard, 
      ShowsUserLocation = true, 
      ZoomEnabled = true, 
      ScrollEnabled = true 
     }; 
     //_map = new MKMapView(mapsView.Bounds); 
     // _map.ShowsUserLocation = true; 
     _mapDelegate = new MapDelegate(); 
     _map.Delegate = _mapDelegate; 
     //mapsView.Add(_map); 
     View = _map; 
     var target = new CLLocationCoordinate2D(30.3753, 69.3451); 
     var viewPoint = new CLLocationCoordinate2D(21.3891, 39.8579); 
     var annotation = new mapAnnotation(new CLLocationCoordinate2D(30.3753, 69.3451), "Pakistan", "Countery of love"); 
     _map.AddAnnotation(annotation); 
     var annotation1 = new mapAnnotation(new CLLocationCoordinate2D(21.3891, 39.8579), "Makka", "Allah home"); 
     _map.AddAnnotation(annotation1); 
     var camera = MKMapCamera.CameraLookingAtCenterCoordinate(target, viewPoint, 500); 
     _map.Camera = camera; 
     createRoute(); 
     //CLLocationCoordinate2D coords = new CLLocationCoordinate2D(30.3753, 69.3451); 
     //MKCoordinateSpan span = new MKCoordinateSpan(MilesToLatitudeDegrees(20), MilesToLongitudeDegrees(20, coords.Latitude)); 
     //_map.Region = new MKCoordinateRegion(coords, span); 
    } 

    public void createRoute() 
    { 

     var dict = new NSDictionary(); 
     var orignPlaceMark = new MKPlacemark(new CLLocationCoordinate2D(30.3753, 69.3451), dict); 
     var sourceItem = new MKMapItem(orignPlaceMark); 

     //End at Xamarin Cambridge Office 
     var destPlaceMark = new MKPlacemark(new CLLocationCoordinate2D(21.3891, 39.8579), dict); 
     var destItem = new MKMapItem(destPlaceMark); 

     var request = new MKDirectionsRequest 
     { 
      Source = sourceItem, 
      Destination = destItem, 
      RequestsAlternateRoutes = true, 

     }; 

     var directions = new MKDirections(request); 

     directions.CalculateDirections((response, error) => 
     { 
      if (error != null) 
      { 
       Console.WriteLine(error.LocalizedDescription); 
      } 
      else 
      { 
       //Add each Polyline from route to map as overlay 
       foreach (var route in response.Routes) 
       { 
        _map.AddOverlay(route.Polyline); 
       } 

      } 
     }); 
    } 

MapDelegate Код:

class MapDelegate : MKMapViewDelegate 
    { 
     public override MKOverlayRenderer OverlayRenderer(MKMapView mapView, IMKOverlay overlay) 
     { 
      if (overlay is MKPolyline) 
      { 
       var route = (MKPolyline)overlay; 
       var renderer = new MKPolylineRenderer(route) { StrokeColor = UIColor.Blue }; 
       return renderer; 
      } 
      return null; 
     } 
     public override MKOverlayView GetViewForOverlay(MKMapView mapView, IMKOverlay overlay) 
     { 
      if (overlay is MKPolyline) 
      { 
       // return a view for the polygon 
       MKPolyline l_polyline = overlay as MKPolyline; 
       MKPolylineView l_polylineView = new MKPolylineView(l_polyline); 
       MKPolylineRenderer l_polylineRenderer = new MKPolylineRenderer(l_polyline); 

       l_polylineView.FillColor = UIColor.Blue; 
       l_polylineView.StrokeColor = UIColor.Red; 

       return l_polylineView; 
      } 

      return null; 
     } 
    } 

First pic is the Starting point Second pic is ending point

ответ

0

Проблема заключается в методе GetViewForOverlay,

наложения параметр не не типа MKPolyline, это обертка, содержащая MKPolyline, это как получить его:

MKOverlayRenderer GetOverlayRenderer(MKMapView mapView, IMKOverlay overlayWrapper) 
{ 
    var overlay = Runtime.GetNSObject(overlayWrapper.Handle) as IMKOverlay; 

    ... 
} 

Source from xamarin forum