Issue
I have e CustomMapRenderer
on iOS project and I want to add two more fields in the marker click.
In the CustomMKAnnotationView.cs
I create a two more objects – CodeNum
and AlertLevel
:
using MapKit;
namespace MaritsaTundzhaForecast.iOS
{
public class CustomMKAnnotationView : MKAnnotationView
{
public string Name { get; set; }
public string Url { get; set; }
public int AlertLevel { get; set; }
public int CodeNum { get; set; }
public CustomMKAnnotationView(IMKAnnotation annotation, string id)
: base(annotation, id)
{
}
}
}
In CustomMapRenderer.cs
I use this line of code to show it but when I click on the pin they do not appear:
((CustomMKAnnotationView)annotationView).AlertLevel customPin.AlertLevel;
((CustomMKAnnotationView)annotationView).CodeNum customPin.CodeNum;
This is the full code of GetViewForAnnotation
:
protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
{
MKAnnotationView annotationView null;
if (annotation is MKUserLocation)
return null;
var customPin GetCustomPin(annotation as MKPointAnnotation);
if (customPin null)
{
throw new Exception("Custom pin not found");
}
annotationView mapView.DequeueReusableAnnotation(customPin.Name);
if (annotationView null)
{
annotationView new CustomMKAnnotationView(annotation, customPin.Name);
annotationView.Image UIImage.FromFile("pin.png");
annotationView.CalloutOffset new CGPoint(0, 0);
annotationView.LeftCalloutAccessoryView new UIImageView(UIImage.FromFile("green.png"));
((CustomMKAnnotationView)annotationView).Name customPin.Name;
((CustomMKAnnotationView)annotationView).Url customPin.Url;
((CustomMKAnnotationView)annotationView).AlertLevel customPin.AlertLevel;
((CustomMKAnnotationView)annotationView).CodeNum customPin.CodeNum;
}
annotationView.CanShowCallout true;
return annotationView;
}
I have OnDidSelectAnnotation
methods, but I don’t know what to write inside to display CodeNum
and AlertLevel
:
void OnDidSelectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
CustomMKAnnotationView customView e.View as CustomMKAnnotationView;
customPinView new UIView();
if (customView.Name.Equals("Xamarin"))
{
customPinView.Frame new CGRect(0, 0, 200, 84);
/*
var image new UIImageView(new CGRect(0, 0, 200, 84));
image.Image UIImage.FromFile("green.png");
customPinView.AddSubview(image);
*/
customPinView.Center new CGPoint(0, -(e.View.Frame.Height + 75));
e.View.AddSubview(customPinView);
}
}
void OnDidDeselectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
if (!e.View.Selected)
{
customPinView.RemoveFromSuperview();
customPinView.Dispose();
customPinView null;
}
}
I use this code to create a new label but I want to put this values from customView.AlertLevel.ToString();
inside in the info window.
void OnDidSelectAnnotationView(object sender, MKAnnotationViewEventArgs e)
{
CustomMKAnnotationView customView e.View as CustomMKAnnotationView;
customPinView new UIView();
if (customView.Name.Equals("Xamarin"))
{
customPinView.Frame new CGRect(0, 0, 200, 84);
/*
var image new UIImageView(new CGRect(0, 0, 200, 84));
image.Image UIImage.FromFile("green.png");
customPinView.AddSubview(image);
*/
customPinView.Center new CGPoint(0, -(e.View.Frame.Height + 75));
e.View.AddSubview(customPinView);
var label new UILabel(new CGRect(0, 0, 200, 84));
label.Text customView.AlertLevel.ToString();
customPinView.AddSubview(label);
}
}
This is screenshot how look now:
Solution
you need to modify the UI to display the additional data
// you will need to experiment with the Bounds to fit your UI
var label new UILabel(new CGRect(0, 0, 100, 50));
label.Text customView.AlertLevel;
customPinView.AddSubview(label);
Answered By – Jason