点击选择MKPolygon

问题描述 投票:1回答:1

我试图允许用户使用xamarin表单iOS在地图上选择多边形,并对他们选择的那些应用笔划。我无法弄清楚如何用c#创建一个轻击手势。

我正在创建多边形

var blockOverlay = MKPolygon.FromCoordinates(coords);
 Constants.nativeMap.AddOverlay(blockOverlay);

理想情况下,我希望它看起来像我的Android Map

c# xamarin xamarin.forms xamarin.ios
1个回答
0
投票

您可以将UITapGestureRecognizer添加到MKMapView并将捕获的抽头坐标转换为地图点,并测试它是否存在于任何叠加层中。

Example:

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作为叠加层,如果没有,请相应调整。

© www.soinside.com 2019 - 2024. All rights reserved.