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 ?
See Question&Answers more detail:
os