Добавление вопросов и ответов в Pin Xamarin.forms - PullRequest
0 голосов
/ 14 апреля 2020

Есть ли способ сделать всплывающее окно с несколькими вопросами + несколькими ответами на выбор после нажатия добавленной булавки на карте? Как это сделать? Больше всего я имею в виду, как комбинировать всплывающее окно с пин-кодом (в xamarin.forms). Я извиняюсь за слабый язык

Мой пин-код для добавления пинов:

private async void OnButton(object sender, EventArgs e) 
    {

        Pin pin = new Pin
        {

            Label = "Nazwa",
            Address = "adres",
            Type = PinType.SavedPin,
            Position = new Position(Convert.ToDouble(szerokosc.Text), Convert.ToDouble(dlugosc.Text))

        };
        positions.Add(new Position(Convert.ToDouble(szerokosc.Text), Convert.ToDouble(dlugosc.Text)));
        Polyline polyline = new Polyline
        {
            StrokeColor = Color.Blue,
            StrokeWidth = 6
        };
        foreach (Position pos in positions)
        {
            polyline.Geopath.Add(pos);

        }
        maps.Pins.Add(pin);
        maps.MapElements.Add(polyline);

        var json = JsonConvert.SerializeObject(new { X = pin.Position.Latitude, Y = pin.Position.Longitude });

        var content = new StringContent(json, Encoding.UTF8, "application/json");

        HttpClient client = new HttpClient();

        var result = await client.PostAsync("URL", content);

        if (result.StatusCode == HttpStatusCode.Created)
        {
            await DisplayAlert("Komunikat", "Dodanie puntku przebiegło pomyślnie", "Anuluj");
        }

    }

1 Ответ

0 голосов
/ 15 апреля 2020

Вы можете использовать CustomRenderer для определения вашей карты и создания настраиваемой информационной базы на основе ваших потребностей. Например, используйте просмотр списка для отображения ваших вопросов и ответов.

Вы можете сослаться на custom PIN-код карты и окно пользовательской информации

Обновление (я могу дать только некоторые фрагменты, вы должны заполнить свой контент и базу макетов в соответствии с вашими потребностями):

[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace CustomRenderer.Droid
{
  public class CustomMapRenderer : MapRenderer, GoogleMap.IInfoWindowAdapter
  {
    List<CustomPin> customPins;

    public CustomMapRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null)
        {
            NativeMap.InfoWindowClick -= OnInfoWindowClick;
        }

        if (e.NewElement != null)
        {
            var formsMap = (CustomMap)e.NewElement;
            customPins = formsMap.CustomPins;
        }
    }

    protected override void OnMapReady(GoogleMap map)
    {
        base.OnMapReady(map);

        NativeMap.InfoWindowClick += OnInfoWindowClick;
        NativeMap.SetInfoWindowAdapter(this);
    }

    protected override MarkerOptions CreateMarker(Pin pin)
    {
        var marker = new MarkerOptions();
        marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
        marker.SetTitle(pin.Label);
        marker.SetSnippet(pin.Address);
        marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.pin));
        return marker;
    }

    void OnInfoWindowClick(object sender, GoogleMap.InfoWindowClickEventArgs e)
    {
        var customPin = GetCustomPin(e.Marker);
        if (customPin == null)
        {
            throw new Exception("Custom pin not found");
        }

        if (!string.IsNullOrWhiteSpace(customPin.Url))
        {
            var url = Android.Net.Uri.Parse(customPin.Url);
            var intent = new Intent(Intent.ActionView, url);
            intent.AddFlags(ActivityFlags.NewTask);
            Android.App.Application.Context.StartActivity(intent);
        }
    }

    // you could custom your view with a listview here,fill the answers and question
    public Android.Views.View GetInfoContents(Marker marker)
    {
        var inflater = Android.App.Application.Context.GetSystemService(Context.LayoutInflaterService) as Android.Views.LayoutInflater;
        if (inflater != null)
        {
            Android.Views.View view;

            var customPin = GetCustomPin(marker);
            if (customPin == null)
            {
                throw new Exception("Custom pin not found");
            }

            //inflate your custom layout
            if (customPin.Name.Equals("Xamarin"))
            {

                view = inflater.Inflate(Resource.Layout.XamarinMapInfoWindow, null);
            }
            else
            {
                view = inflater.Inflate(Resource.Layout.MapInfoWindow, null);
            }

            var infoTitle = view.FindViewById<TextView>(Resource.Id.InfoWindowTitle);
            var infoSubtitle = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle);

            if (infoTitle != null)
            {
                infoTitle.Text = marker.Title;
            }
            if (infoSubtitle != null)
            {
                infoSubtitle.Text = marker.Snippet;
            }

            return view;
        }
        return null;
    }

    public Android.Views.View GetInfoWindow(Marker marker)
    {
        return null;
    }

    CustomPin GetCustomPin(Marker annotation)
    {
        var position = new Position(annotation.Position.Latitude, annotation.Position.Longitude);
        foreach (var pin in customPins)
        {
            if (pin.Position == position)
            {
                return pin;
            }
        }
        return null;
    }
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...