Длинное нажатие и выпадение булавки на картах форм xamarin - PullRequest
0 голосов
/ 16 января 2019

Я использовал пакет nuget Xamarin.Forms.Maps и отображал карту на устройстве. Я могу показать пин-код на нажатии внешней кнопки с помощью следующего кода, но не могу добиться того же на касании карты, чтобы сбросить булавку в определенном месте.

public void addPin(double latitude, double longitude, string labelName)
{
     Position position = new Position(latitude, longitude);
    _assignedPin = new Pin
    {
        Type = PinType.Place,
        Position = position,
        Label = labelName,
        Address = "custom detail info"
    };
    map.Pins.Add(_assignedPin);
}

Я подписался на этот блог , чтобы получить лат на карте, но карта не отображает булавку на карте.

1 Ответ

0 голосов
/ 25 января 2019

Нам нужно добавить код в самом рендерере, чтобы удалить пин-код, используя xamarin.forms.maps

В Android: класс рендерера:

private void googleMap_MapClick(object sender, GoogleMap.MapClickEventArgs e)
{
    Map.Pins.Add(new Pin
    {
        Label = "Pin from tap",
        Position = new Position(e.Point.Latitude, e.Point.Longitude))
    }
}

А в классе iOS Renderer:

[assembly: ExportRenderer(typeof(ExtMap), typeof(ExtMapRenderer))]
namespace Xamarin.iOS.CustomRenderers
{
    /// <summary>
    /// Renderer for the xamarin ios map control
    /// </summary>
    public class ExtMapRenderer : MapRenderer
    {
        private readonly UITapGestureRecognizer _tapRecogniser;

        public ExtMapRenderer()
        {
            _tapRecogniser = new UITapGestureRecognizer(OnTap)
            {
                NumberOfTapsRequired = 1,
                NumberOfTouchesRequired = 1
            };
        }

        protected override IMKAnnotation CreateAnnotation(Pin pin)
        {
            return base.CreateAnnotation(pin);
        }



        class BasicMapAnnotation : MKAnnotation
        {
            CLLocationCoordinate2D coord;
            string title, subtitle;

            public override CLLocationCoordinate2D Coordinate { get { return coord; } }
            public override void SetCoordinate(CLLocationCoordinate2D value)
            {
                coord = value;
            }
            public override string Title { get { return title; } }
            public override string Subtitle { get { return subtitle; } }
            public BasicMapAnnotation(CLLocationCoordinate2D coordinate, string title, string subtitle)
            {
                this.coord = coordinate;
                this.title = title;
                this.subtitle = subtitle;
            }
        }



        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
        }

        private async void OnTap(UITapGestureRecognizer recognizer)
        {
            var cgPoint = recognizer.LocationInView(Control);
            var nativeMap = Control as MKMapView;

            var location = ((MKMapView)Control).ConvertPoint(cgPoint, Control);

            ((ExtMap)Element).OnTap(new Position(location.Latitude, location.Longitude));

            try
            {
                var lat = location.Latitude;
                var lon = location.Longitude;

                var placemarks = await Geocoding.GetPlacemarksAsync(lat, lon);

                var placemark = placemarks?.FirstOrDefault();
                if (placemark != null)
                {
                    var geocodeAddress =
                        $"AdminArea:       {placemark.AdminArea}\n" +
                        $"CountryCode:     {placemark.CountryCode}\n" +
                        $"CountryName:     {placemark.CountryName}\n" +
                        $"FeatureName:     {placemark.FeatureName}\n" +
                        $"Locality:        {placemark.Locality}\n" +
                        $"PostalCode:      {placemark.PostalCode}\n" +
                        $"SubAdminArea:    {placemark.SubAdminArea}\n" +
                        $"SubLocality:     {placemark.SubLocality}\n" +
                        $"SubThoroughfare: {placemark.SubThoroughfare}\n" +
                        $"Thoroughfare:    {placemark.Thoroughfare}\n";

                    Console.WriteLine(geocodeAddress);
                    var annotation = new BasicMapAnnotation(new CLLocationCoordinate2D(lat, lon), placemark.Thoroughfare, placemark.SubThoroughfare);
                    nativeMap.AddAnnotation(annotation);
                }
            }
            catch (FeatureNotSupportedException fnsEx)
            {
                // Feature not supported on device
                Console.WriteLine(fnsEx);
            }
            catch (Exception ex)
            {
                // Handle exception that may have occurred in geocoding
                Console.WriteLine(ex);
            }




        }

        protected override void OnElementChanged(ElementChangedEventArgs<View> e)
        {
            if (Control != null)
                Control.RemoveGestureRecognizer(_tapRecogniser);

            base.OnElementChanged(e);

            if (Control != null)
                Control.AddGestureRecognizer(_tapRecogniser);
        }
    }
}
...