Skip to content Skip to sidebar Skip to footer

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: pub

Solution 1:

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

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)
        {
            thrownew 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 alertlevelstring 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 xmlif (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;
    }
    returnnull;
}

Update :

publicpartialclassYouPage: ContentPage
{
    publicYouPage()
    {
        InitializeComponent();
    }

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

Post a Comment for "How To Set Second Label On Infowindow Xamarin Map"