在Pin Xamarin.forms中添加问题和答案 [英] Adding questions and answers to Pin Xamarin.forms

查看:153
本文介绍了在Pin Xamarin.forms中添加问题和答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按下地图上添加的图钉后,是否有任何方法可以弹出带有几个问题+几个答案的选项? 怎么做?我最主要的意思是如何将弹出窗口与图钉结合(在xamarin.forms中) 我为我的语言不好而道歉

Is there any way to make a popup with a few questions + a few answers to choose from after pressing the added pin on the map? How to do that? What I mean most is how to combine the popup with the pin (in xamarin.forms) I apologize for my weak language

我用于添加密码的密码:

My pin code for adding pins:

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

    }

推荐答案

您可以使用CustomRenderer定义地图,并根据需要创建自定义信息窗口.例如,使用列表视图显示您的问题和答案.

You could use CustomRenderer to define your Map,and create a custom info window base on your needs.For example,use a listview to display your questions and answers.

您可以参考 更新(我只能给您一些片段,您应该根据需要填写内容和布局):

Update (i could only give you some snippets,you should fill your content and layout base on your needs):

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

这篇关于在Pin Xamarin.forms中添加问题和答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆