问题描述
我目前正在关注本教程,用于向地图添加多边形.我需要能够在地图上添加多个多边形,所以我对代码进行了一些改动,以便可以使用addOverlays
来接受一个IMKOverlay
对象数组,而不是一个addOverlay
来接受一个多边形IMKOverlay
对象.
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
的叠加层.有一些基本的内置类型,例如MKCircle
,MKPolygon
等.但是您也可以设计自己的叠加层.即用于定义恶劣天气(闪电,暴风云,龙卷风等)位置的叠加层.这些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在地图上有多个多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!