简体   繁体   English

Xamarin.Forms.Maps自定义折线渲染器在视图中不渲染

[英]Xamarin.Forms.Maps custom polyline renderer does not render when in view

I am trying to follow this guide in order to draw a polyline on a map in a Xamarin.Forms app. 我正在尝试遵循本指南 ,以便在Xamarin.Forms应用程序的地图上绘制折线。 It should track the user's position in real-time and update the polyline when new position data comes in. 它应该实时跟踪用户的位置,并在输入新的位置数据时更新折线。

I wrote a custom map renderer that will render the polyline, but for some reason it does not update when the map is in view. 我编写了一个自定义地图渲染器,它将渲染折线,但是由于某些原因,它在地图显示时不会更新。 I have to navigate back to the main launch page and navigate to the mapping page again for it to update. 我必须导航回到主启动页面并再次导航到映射页面以进行更新。

I extracted the minimum code to reproduce the problem, but it is still too much to paste here, so I hosted it on GitHub: 我提取了最少的代码来重现该问题,但是在此处粘贴仍然太多,因此我将其托管在GitHub上:

https://github.com/Steztric/MapWithWaylineSample https://github.com/Steztric/MapWithWaylineSample

Please could somebody let me know what I am doing wrong. 请有人让我知道我在做什么错。 You can demonstrate the problem by cloning the repo and running it. 您可以通过克隆存储库并运行它来演示问题。

You need to create renderer every time, so remove class variable polylineRenderer and use local one. 您每次都需要创建渲染器,因此请删除类变量polylineRenderer并使用本地变量。

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

            if (overlay is MKPolyline)
            {
                var polylineRenderer = new MKPolylineRenderer(overlay as MKPolyline);
                polylineRenderer.FillColor = UIColor.Blue;
                polylineRenderer.StrokeColor = UIColor.Red;
                polylineRenderer.LineWidth = 3;
                polylineRenderer.Alpha = 0.4f;
                return polylineRenderer;
            }
            else
            {
                return null;
            }
        }

Also you can simplify things a little 你也可以简化一些事情

Define MKPolyline currentWayline; 定义MKPolyline currentWayline; then 然后

    var wayline = MKPolyline.FromCoordinates(coords.ToArray());
    //IMKOverlay overlay = Runtime.GetNSObject(wayline.Handle) as IMKOverlay;
    nativeMap.AddOverlay(wayline);
    currentWayline = wayline;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM