如何在不点击Xamarin.Forms的情况下显示Pin的标签(在地图上)

时间:2016-05-14 14:06:13

标签: xamarin xamarin.forms

在Xamarin.Forms中将引脚添加到地图时,如何使引脚默认显示标签(不单击它)。

map.MoveToRegion(MapSpan.FromCenterAndRadius(position, Distance.FromMiles(0.4)));
   var pin = new Pin
  {
   Type = PinType.Place,
   Position = position,
   Label = "Some Text",

   };
   map.Pins.Add(pin);

1 个答案:

答案 0 :(得分:3)

您可以通过自定义地图渲染来完成。

例如,在iOS上,您可以向MKMapView控件添加两个代理:

  • DidAddAnnotationViews:每次添加MKAnnotation时,都要预先选择它们。
  • DidDeselectAnnotationView:如果有人/某物试图取消选择MKAnnotation,只需重新选择它们......

工作实例作为起点:

[assembly: ExportRenderer(typeof(PinViewMap), typeof(PinViewMapRenderer))]
namespace WorkingWithMaps.iOS
{
    public class PinViewMapRenderer : MapRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                var map = Control as MKMapView;
                map.DidDeselectAnnotationView += (object sender, MKAnnotationViewEventArgs eventArgs) =>
                {
                    foreach (var anno in ((MKMapView)sender).Annotations)
                    {
                        ((MKMapView)sender).SelectAnnotation(anno, true);
                    }
                };
                map.DidAddAnnotationViews += (object sender, MKMapViewAnnotationEventArgs eventArgs) =>
                {
                    foreach (var anno in ((MKMapView)sender).Annotations)
                    {
                        ((MKMapView)sender).SelectAnnotation(anno, true);
                    }

                };
            }
        }
    }
}