将多个折线添加到MKMapView

将多个折线添加到MKMapView

本文介绍了将多个折线添加到MKMapView(Xamarin)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在扩展提供的示例@ https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/maps/map-overlay/polyline/将多段线添加到我的地图中.

I'm extending the example provided @ https://developer.xamarin.com/recipes/cross-platform/xamarin-forms/maps/map-overlay/polyline/ to add multiple polylines to my map.

任何一条折线都可以正常工作,但是当我添加第二条折线或在注释旁边添加一条折线时,GetOverlayRenderer会因错误而出错

Everything works fine with a single polyline, but when I add the second, or add a polyline alongside an annotation, GetOverlayRenderer errors out with the error

"Value cannot be null.Parameter name: polyline"

它在网上出现了错误:

polylineRenderer = new MKPolylineRenderer (overlay as MKPolyline);

当我查看叠加层对象时,它是:

And when I look at the overlay object, it is:

{MapKit.MKOverlayWrapper}
BoundingMapRect: {{{62634939.7333333, 111606556.311973}, {745654.044444449, 848491.772016793}}}
Coordinate: {CoreLocation.CLLocationCoordinate2D}
Handle: 0x791ca560
Non-public members:

我不明白为什么它可以与一条折线一起使用,但是当我在地图上添加其他任何内容时,它就会失败(也是第一次通过该方法)

I don't understand why it works with one polyline, but when I add anything else to the map, it fails (first time through the method, also)

这里是发生错误的整个方法.我可以提供更多代码,但它仅适用于一行,而不能适用于两行. (我遍历对象列表...如果有1,就可以了.如果有2,则失败了.)

Here's the entire method where the error is occurring. I can provide more code but it works FINE with one line, just not with two. (I loop through a list of objects... if there's 1, it's fine.. if there is 2, it fails).

MKOverlayRenderer GetOverlayRenderer (MKMapView mapView, IMKOverlay overlay)
    {

        if (polylineRenderer == null) {
            try
            {

            polylineRenderer = new MKPolylineRenderer (overlay as MKPolyline);
            polylineRenderer.FillColor = UIColor.Blue;
            polylineRenderer.StrokeColor = UIColor.Red;
            polylineRenderer.LineWidth = 3;
            polylineRenderer.Alpha = 0.4f;
            }
            catch (Exception ex) {
            }
        }
        return polylineRenderer;
    }

推荐答案

最终结果是,传递给该方法的覆盖层是各种包装,并且您需要获取"handle"属性以获取实际的覆盖层

The end result here was that the overlay passed to the method is a wrapper of sorts, and you need to get the "handle" property to get the actual overlay.

public override MKOverlayView GetViewForOverlay (MKMapView mapView, IMKOverlay overlayWrapper)
    {
    if (overlayWrapper != null) {
    var overlay = Runtime.GetNSObject(overlayWrapper.Handle) as MKOverlay;
    // Do Overlay Functions here
    }

此消息的来源来自 https://forums.xamarin.com/discussion/31616/what-is-mkoverlaywrapper-its-breaking-my-map-renderer

这篇关于将多个折线添加到MKMapView(Xamarin)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 19:43