Xamarin-iOS在地图上有多个多边形 [英] Xamarin - iOS Multiple polygons on map

查看:82
本文介绍了Xamarin-iOS在地图上有多个多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在关注

I am currently following this tutorial for adding a polygon to a map. I need to be able to add multiple polygons to my map, so I have slightly altered the code so that I can use addOverlays which takes in an array of IMKOverlay objects instead of one addOverlay which just takes in a single IMKOverlay object.

但是这不起作用...它只会在地图上绘制第一个多边形!

This doesn't work however... It only draws the first polygon on the map!

void addPolygonsToMap()
        {
            overlayList = new List<IMKOverlay>();
            for (int i = 0; i < polygons.Count; i++)
            {

                    CLLocationCoordinate2D[] coords = new CLLocationCoordinate2D[polygons[i].Count];

                    int index=0;
                    foreach (var position in polygons[i])
                    {
                        coords[index] = new CLLocationCoordinate2D(position.Latitude, position.Longitude);
                        index++;
                    }
                    var blockOverlay = MKPolygon.FromCoordinates(coords);
                    overlayList.Add(blockOverlay);

            }
            IMKOverlay[] imko = overlayList.ToArray();
            nativeMap.AddOverlays(imko);

        }

讨论中,似乎每次需要向地图添加另一个多边形时,我都必须调用MKPolygonRenderer的新实例,但是我不确定该示例如何转换为我的代码.这是我的MKPolygonRenderer函数:

In this discussion, it would appear that I have to call a new instance of MKPolygonRenderer each time I need to add another polygon to my map, but I'm unsure how this example translates to my code. Here is my MKPolygonRenderer function:

MKOverlayRenderer GetOverlayRenderer(MKMapView mapView, IMKOverlay overlayWrapper)
  {
      if (polygonRenderer == null && !Equals(overlayWrapper, null)) {
          var overlay = Runtime.GetNSObject(overlayWrapper.Handle) as IMKOverlay;
          polygonRenderer = new MKPolygonRenderer(overlay as MKPolygon) {
              FillColor = UIColor.Red,
              StrokeColor = UIColor.Blue,
              Alpha = 0.4f,
              LineWidth = 9
          };
      }
      return polygonRenderer;
  }

推荐答案

每次调用OverlayRenderer时都创建一个新的渲染器实例,无需将渲染器缓存在类级别变量中,因为MKMapView将缓存渲染器.

Create a new renderer instance each time OverlayRenderer is called, there is no need to cache the renderer in a class level variable as the MKMapView will cache the renderers as needed.

class MyMapDelegate : MKMapViewDelegate
{
    public override MKOverlayRenderer OverlayRenderer(MKMapView mapView, IMKOverlay overlay)
    {
        switch (overlay)
        {
            case MKPolygon polygon:
                var prenderer = new MKPolygonRenderer(polygon)
                {
                    FillColor = UIColor.Red,
                    StrokeColor = UIColor.Blue,
                    Alpha = 0.4f,
                    LineWidth = 9
                };
                return prenderer;
            default:
                throw new Exception($"Not supported: {overlay.GetType()}");
        }
    }
}

实例,并将委托分配给您的地图:

mapDelegate = new MyMapDelegate();
map.Delegate = mapDelegate;

注意:由于您不想获取GC的信息,因此将MyMapDelegate的实例存储在类级别的变量中

Note: Store the instance of your MyMapDelegate in a class level variable as you do not want to get GC'd

MKMapView涉及两个步骤,以在其地图上显示覆盖图.

MKMapView has two steps involved to display an overlay on its map.

1. Calling `AddOverlay` and `AddOverlays`

首先,您要向地图添加符合IMKOverlay的叠加层.有一些基本的内置类型,例如MKCircleMKPolygon等.但是您也可以设计自己的叠加层.即用于定义恶劣天气(闪电,暴风云,龙卷风等)位置的叠加层.这些MKOverlays描述了该项目的地理位置,但不是如何绘制该项目.

First you add overlays to the map that conform to IMKOverlay. There are basic built-in types such as MKCircle, MKPolygon, etc... but you can also design your own overlays; i.e. overlays that define the location of severe weather (lightning, storm clouds, tornados, etc..). These MKOverlays describe the geo-location of the item but not how to draw it.

2. Responding to `OverlayRenderer` requests

当地图的显示区域与其中一个叠加层相交时,地图需要将其绘制在屏幕上.调用地图的委托(您的MKMapViewDelegate子类)以提供一个MKOverlayRenderer,该MKOverlayRenderer定义了绘制例程以在地图上绘制叠加层.

When the display area of the map intersects with one of the overlays, the map need to draw it on the screen. The map's delegate (your MKMapViewDelegate subclass) is called to supply a MKOverlayRenderer that defines the drawing routines to paint the overlay on the map.

此图涉及使用Core Graphics例程(UIKit可以有一些限制)将叠加层的地理坐标转换为本地显示坐标(可用的辅助方法).可以使用MKCircleRenderer,MKPolygonRenderer等基本的内置渲染器.可以使用这些渲染器,也可以编写自己的MKOverlayRenderer子类.

This drawing involves converting the geo-coordinates of the overlay to local display coordinates (helper methods are available) using Core Graphics routines (UIKit can be used with some limitations). There are basic built-in renderers for MKCircleRenderer, MKPolygonRenderer, etc.. that can be used or you can write your own MKOverlayRenderer subclass.

您可以提供一种自定义方式来渲染MKCircle叠加层,可能是目标样式的红色/白色多环靶心,而不是默认的圆形渲染器对其进行绘制的方式,或者是在其中绘制严重风暴符号的自定义渲染器MKPolygon的边界以匹配您的自定义严重风暴覆盖.

You could supply a custom way to renderer a MKCircle overlay, maybe a target-style red/white multi-ringed bullseye, instead of the way the default circle renderer draws it, or custom renderers that draw severe storm symbols within the bounds of a MKPolygon to match your custom severe storm overlays.

我的示例代码:

由于您正在使用MKPolygon来构建叠加层,因此可以使用MKPolygonRenderer来显示它们.在我的示例中,我提供了一个模式匹配开关(C#6),它为添加到地图的每个 MKPolygon返回一个半透明的Red/Blue MKPolygonRenderer(如果您添加了基于非MKPolygon的覆盖它将引发异常).

Since you are using MKPolygon to build your overlays, you can use the MKPolygonRenderer to display them. In my example, I provide a pattern matching switch (C# 6) that returns a semi-transparent Red/Blue MKPolygonRenderer for every MKPolygon that you added to the map (if you added a non-MKPolygon based overlay it will throw an exception).

这篇关于Xamarin-iOS在地图上有多个多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆