为什么多段线没有显示



下面是我的全部代码,显示一行简单的代码:

public class MapUIViewController : UIViewController
{
MKMapView map;
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
CLLocationCoordinate2D loc = new CLLocationCoordinate2D(38.8895, -77.036168);
map = new MKMapView()
{
MapType = MKMapType.Hybrid,
Region = new MKCoordinateRegion(loc, new MKCoordinateSpan(0.1, 0.1)),
Frame = View.Bounds,
};
View = map;
ShowLine(loc, new CLLocationCoordinate2D(loc.Latitude + 1, loc.Longitude + 1));
}

void ShowLine(CLLocationCoordinate2D start, CLLocationCoordinate2D end)
{
MKGeodesicPolyline polyline = MKGeodesicPolyline.FromCoordinates(new CLLocationCoordinate2D[] { start, end });
map.AddOverlay(polyline);
map.OverlayRenderer = rend;
}
MKOverlayRenderer rend(MKMapView mapView, IMKOverlay overlay)
{
if (overlay is MKGeodesicPolyline line)
return new MKPolylineRenderer(line) { LineWidth = 3, StrokeColor = UIColor.Red, FillColor = UIColor.Brown, Alpha = 1 };
else
return null;
}
}

它显示指定的位置,但不显示直线。

(代码在C#中(使用Xamarin.ios(,但在Swift和Objective C中应该类似,我的问题是渲染器,它是ios功能,而不是语言功能(。

我一定错过了什么,但找不到。

添加行之前,需要指定渲染器。另一种方式意味着添加线时不存在渲染器,因此不会绘制

错误的

map.AddOverlay(polyline);
map.OverlayRenderer = rend;

右侧

map.OverlayRenderer = rend;
map.AddOverlay(polyline);

最新更新