我CustomMapRenderer
在 iOS 项目上有 e ,我想在标记单击中再添加两个字段。
在CustomMKAnnotationView.cs
我创建了另外两个对象 -CodeNum
和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)
{
}
}
}
在CustomMapRenderer.cs
我使用这行代码来显示它但是当我点击 pin 时它们不会出现:
((CustomMKAnnotationView)annotationView).AlertLevel = customPin.AlertLevel;
((CustomMKAnnotationView)annotationView).CodeNum = customPin.CodeNum;
这是完整的代码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;
}
我有OnDidSelectAnnotation
方法,但我不知道在里面写什么来显示CodeNum
和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;
}
}
您需要修改 UI 以显示附加数据
// 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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句