如何在InfoWindow xamarin地图上设置第二个标签 [英] How to set second label on InfoWindow xamarin map

查看:75
本文介绍了如何在InfoWindow xamarin地图上设置第二个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在xamarin地图的信息窗口中设置第二个标签.我使用的是

解决方案

您可以使用自定义渲染器中的 GetCustomPin 方法获取 CustomPin ,就像上面的示例一样链接.

  CustomPin GetCustomPin(标记注释){var position = new Position(annotation.Position.Latitude,annotation.Position.Longitude);foreach(customPins中的var引脚){如果(pin.Position == position){回销}}返回null;} 

,并在您的公开Android.Views.View GetInfoContents(标记标记)方法中:

 公共Android.Views.View GetInfoContents(标记标记){var inflater = Android.Apps.Application.Context.GetSystemService(Context.LayoutInflaterService)as Android.Views.LayoutInflater;如果(充气器!= null){Android.Views.View视图;var customPin = GetCustomPin(marker);如果(customPin == null){抛出新的异常(找不到自定义引脚");}如果(customPin.Name.Equals("Xamarin")){view = inflater.Inflate(Resource.Layout.XamarinMapInfoWindow,null);}别的{view = inflater.Inflate(Resource.Layout.MapInfoWindow,null);}CustomPin pin = GetCustomPin(marker);int CodeNum = pin.CodeNum;//获取密码,然后获取密码和警报级别字符串AlertLevel = pin.AlertLevel;var infoTitle = view.FindViewById< TextView>(Resource.Id.InfoWindowTitle);var infoSubtitle = view.FindViewById< TextView>(Resource.Id.InfoWindowSubtitle);var infoSubtitle2 = view.FindViewById< TextView>(Resource.Id.InfoWindowSubtitle2);var infoSubtitle3 = view.FindViewById< TextView>(Resource.Id.InfoWindowSubtitle3);//在您的xml中创建第三个TextView如果(infoTitle!= null){infoTitle.Text = marker.Title;}如果(infoSubtitle!= null){infoSubtitle.Text = marker.Snippet;}如果(infoSubtitle2!= null){infoSubtitle2.Text = CodeNum +";}如果(infoSubtitle3!= null){infoSubtitle3.Text = AlertLevel;}返回视图}返回null;} 

更新:

 公共局部类YouPage:ContentPage{公开的YouPage(){InitializeComponent();}受保护的异步覆盖 void OnAppearing(){base.OnAppearing();...//您从MySql获取数据,如果有多个数据,则需要循环var codeNum = xxx;var level = xxx;CustomPin引脚=新的CustomPin();pin.CodeNum = codeNum;pin.AlertLevel =级别;yourcustomMap.Pins.Add(pin);} 

I want to set second label in info window on xamarin map. I use this example.

So exactly I want to set one variable who come from date base on info window like a second label:

 public MapPage()
    {
        InitializeComponent();

        DatabaseConnection();

        CustomPin pin1 = new CustomPin
        {
            Type = PinType.Place,
            Position = new Position(41.59043006333251, 24.766286971618303),
            Name = "Xamarin",
            Label = "р. Бяла",
            Address = "гр. Смолян",
        };

        CustomPin pin2 = new CustomPin
        {
            Type = PinType.Place,
            Position = new Position(41.56817473054596, 24.758451447799708),
            Label = "р. Черна",
            Name = "Xamarin",
            Address = "гр. Смолян",
        };

        CustomPin pin3 = new CustomPin
        {
            Type = PinType.Place,
            Position = new Position(41.48398466282902, 24.847715935872973),
            Label = "р. Елховска",
            Name = "Xamarin",
            Address = "гр. Рудозем",
        };

        customMap.CustomPins = new List<CustomPin> {
            pin1,
            pin2,
            pin3,
        };
        customMap.Pins.Add(pin1);
        customMap.Pins.Add(pin2);
        customMap.Pins.Add(pin3);
        customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(41.567797926753485, 25.389703182725665), Distance.FromKilometers(70)));
    }

try
        {
            Conn.Open();
            string query = "SELECT * FROM sel_alert_level s;";
            MySqlCommand myCommand = new MySqlCommand(query, Conn);
            MySqlDataReader myReader;

            myReader = myCommand.ExecuteReader();
            try
            {
                while (myReader.Read())
                {
                    var codeNum = myReader.GetInt32(4);
                    var level = myReader.GetInt32(3);

                    await DisplayAlert("Database Connection", "Connected .." + Environment.NewLine + myReader.GetInt32(0) + Environment.NewLine + myReader.GetString(1) + Environment.NewLine + myReader.GetString(2) + Environment.NewLine + myReader.GetInt32(3) + Environment.NewLine + myReader.GetInt32(4), "OK");
                }
            }
            finally
            {
                myReader.Close();
                Conn.Close();
            }
        }

I want var codeNum = myReader.GetInt32(4); to be on the pin info window.

In my android project in directory resource/layout I have two axml files for the info window:

XamarinMapInfoWindow.axml

and

MapInfoWindow.axml

Inside in both files I create a new TextView for the second label:

  <TextView
            android:id="@id/InfoWindowSubtitle2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="InfoWindowSubtitle2"
            android:textColor="@android:color/black" />

On my CustomMapRenderer.cs file in android project I have method GetInfoContents in which I do not know how to submit the new label.

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

            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);
            var infoSubtitle2 = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle2);

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

            return view;
        }
        return null;
    }

So

` if (infoSubtitle2 != null)
            {
                infoSubtitle2.Text = marker.Snippet;
            }`

is not the correct code. Before this method in the same page I have CreateMarker method who add a new lines on the marker and here I also don't understand how to submit the new line:

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.green));
        return marker;
    }

Finally I create a new object in the Pin class for Alert Level who will come from data base.

 namespace CustomRenderer
{
    public class CustomPin : Pin
    {
        public string Name { get; set; }
        public string Url { get; set; }
        public int CodeNum { get; set; }
        public int AlertLevel { get; set; }
    }
}

My main question is how to put var level = myReader.GetInt32(3); like a second Label on InfoWindow ?

解决方案

You could get the CustomPin with the GetCustomPin method in the custom renderer like the sample in your above link.

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

and in your public Android.Views.View GetInfoContents(Marker marker) method:

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

        if (customPin.Name.Equals("Xamarin"))
        {
            view = inflater.Inflate(Resource.Layout.XamarinMapInfoWindow, null);
        }
        else
        {
            view = inflater.Inflate(Resource.Layout.MapInfoWindow, null);
        }

        CustomPin pin = GetCustomPin(marker);
        int CodeNum  = pin.CodeNum;          //get the pin,then get the codenum and alertlevel
        string AlertLevel  = pin.AlertLevel;

        var infoTitle = view.FindViewById<TextView>(Resource.Id.InfoWindowTitle);
        var infoSubtitle = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle);
        var infoSubtitle2 = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle2);
        var infoSubtitle3 = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle3);// create the third TextView in your xml

        if (infoTitle != null)
        {
            infoTitle.Text = marker.Title;
        }
        if (infoSubtitle != null)
        {
            infoSubtitle.Text = marker.Snippet;
        }
        if (infoSubtitle2 != null)
        {
            infoSubtitle2.Text = CodeNum  +"";
        }
        
        if (infoSubtitle3 != null)
        {
            infoSubtitle3.Text = AlertLevel;
        }

        return view;
    }
    return null;
}

Update :

public partial class YouPage: ContentPage
{
    public YouPage()
    {
        InitializeComponent();
    }

    protected async override void OnAppearing()
    {
        base.OnAppearing();
        ...  //you get the data from MySql,if you have several data,you need a loop
        var codeNum = xxx;
        var level = xxx;
        CustomPin pin = new CustomPin();
        pin.CodeNum = codeNum;
        pin.AlertLevel = level ;
        yourcustomMap.Pins.Add(pin);
    }
      

这篇关于如何在InfoWindow xamarin地图上设置第二个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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