A. Sinha
A. Sinha

Reputation: 2716

How to display Pin's Label (on a Map) without clicking on Xamarin.Forms

How can I make the pin to display label by default (without clicking it) when it is added to the map in 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);

Upvotes: 2

Views: 2372

Answers (1)

SushiHangover
SushiHangover

Reputation: 74209

You can do it via a custom map render.

As an example, on iOS you can add two delegates to the MKMapView control:

  • DidAddAnnotationViews: Any time a MKAnnotation is added, pre-select them all..
  • DidDeselectAnnotationView: If someone/something tries to deselect the MKAnnotation, just re-select them all...

Working Example as a starting point:

[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);
                    }

                };
            }
        }
    }
}

Upvotes: 4

Related Questions