在 xamarin ios 中使用 MkMap 不会出现路线

Posted

技术标签:

【中文标题】在 xamarin ios 中使用 MkMap 不会出现路线【英文标题】:route line does not appear using MkMap in xamarin ios 【发布时间】:2017-01-31 11:47:59 【问题描述】:

我想在 xamarin ios 中使用 mkMap 绘制路线。我的代码工作正常,但没有显示点之间的路线。我的代码如下 我的第一张图片显示了起始注释点,第二张图片显示了结束注释点

地图视图代码:

 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;
        
    

【问题讨论】:

【参考方案1】:

问题出在GetViewForOverlay方法中,

overlay 参数不是 MKPolyline 类型,它是一个包含 MKPolyline 的包装器,获取方法如下:

MKOverlayRenderer GetOverlayRenderer(MKMapView mapView, IMKOverlay overlayWrapper)

    var overlay = Runtime.GetNSObject(overlayWrapper.Handle) as IMKOverlay;

    ...

Source from xamarin forum

【讨论】:

以上是关于在 xamarin ios 中使用 MkMap 不会出现路线的主要内容,如果未能解决你的问题,请参考以下文章

使用 mkmap 加载地图时显示标题

在 UIAlertView(iOS、Xamarin)上添加 UITextView,而不使用任何第三方库

在Xamarin iOS的Visual Studio App Center中构建失败:只有在使用/ unsafe进行编译时才会出现不安全的代码

Xamarin.iOS:Firebase 推送通知不起作用

如何使 MKMap 适合指定的 View-Region?

Xamarin.iOS 中的文件选择器