Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

c# - How to set second label on InfoWindow xamarin map

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 ?

second label

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...