我试图允许用户使用xamarin表单iOS在地图上选择多边形,并对他们选择的那些应用笔划。我无法弄清楚如何用c#创建一个轻击手势。
我正在创建多边形
var blockOverlay = MKPolygon.FromCoordinates(coords);
Constants.nativeMap.AddOverlay(blockOverlay);
理想情况下,我希望它看起来像我的Android Map
您可以将UITapGestureRecognizer
添加到MKMapView
并将捕获的抽头坐标转换为地图点,并测试它是否存在于任何叠加层中。
var uiTapGesture = new UITapGestureRecognizer(tappedGesture =>
{
foreach (MKPolygon polygon in (tappedGesture.View as MKMapView).Overlays)
{
using (var render = new MKPolygonRenderer(polygon))
{
var coord2D = nativeMap.ConvertPoint(tappedGesture.LocationInView(nativeMap), nativeMap);
var mapPoint = MKMapPoint.FromCoordinate(coord2D);
var polyTouched = render.Path.ContainsPoint(render.PointForMapPoint(mapPoint), true);
if (polyTouched)
Console.WriteLine($"tapped: {polygon}");
}
}
});
nativeMap.AddGestureRecognizer(uiTapGesture);
注意:这假设您使用MKPolygon
作为叠加层,如果没有,请相应调整。