如何在单击 xamarin.forms.maps iOS c# 时更改 pin 图标
Posted
技术标签:
【中文标题】如何在单击 xamarin.forms.maps iOS c# 时更改 pin 图标【英文标题】:How to change pin icon on click in xamarin.forms.maps iOS c# 【发布时间】:2021-12-04 12:08:00 【问题描述】:我想在点击图钉时更改图钉图标。
我从OnDidSelectAnnotationView
中的GetViewForAnnotation
方法中复制了这段代码,但我不知道如何返回 annotationView
。
我不确定这是否是正确的方法,但我能否获得一个示例,说明如何在单击标记时更改图标?
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);
customPinView.Center = new CGPoint(0, -(e.View.Frame.Height + 75));
e.View.AddSubview(customPinView);
var pin = GetCustomPin(c_annotation as MKPointAnnotation);
int mapCode = customView.MapCode;
var result = GetDataFromAPI(mapCode);
var result2 = GetInfoFromStation(mapCode);
MessagingCenter.Send<object, IEnumerable<AlertLevel>>(this, "PinSelected", result);
MessagingCenter.Send<object, IEnumerable<StationInfoOnClick>>(this, "StationInfo", result2);
MKAnnotationView annotationView = null;
if (annotationView == null)
annotationView = new CustomMKAnnotationView(c_annotation, pin.Name);
annotationView.CalloutOffset = new CGPoint(0, 0);
((CustomMKAnnotationView)annotationView).Name = pin.Name;
((CustomMKAnnotationView)annotationView).Url = pin.Url;
((CustomMKAnnotationView)annotationView).Address = pin.Address;
//Add First Line
((CustomMKAnnotationView)annotationView).AlertLevel = pin.AlertLevel;
if (pin.AlertLevel == 1)
annotationView.Image = UIImage.FromFile("red.png");
else if (pin.AlertLevel == 2)
annotationView.Image = UIImage.FromFile("yellow.png");
else if (pin.AlertLevel == 3)
annotationView.Image = UIImage.FromFile("orange.png");
else if (pin.AlertLevel == 4)
annotationView.Image = UIImage.FromFile("red.png");
//Add Second Line
((CustomMKAnnotationView)annotationView).CodeNum = pin.CodeNum;
((CustomMKAnnotationView)annotationView).MapCode = pin.MapCode;
annotationView.CanShowCallout = true;
configureDetailView(annotationView);
return annotationView;
我尝试将void OnDidSelectAnnotationView
更改为MKAnnotationView OnDidSelectAnnotationView
我收到此方法的错误:
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
base.OnElementChanged(e);
if (e.OldElement != null)
var nativeMap = Control as MKMapView;
nativeMap.GetViewForAnnotation = null;
nativeMap.CalloutAccessoryControlTapped -= OnCalloutAccessoryControlTapped;
nativeMap.DidSelectAnnotationView -= OnDidSelectAnnotationView;
nativeMap.DidDeselectAnnotationView -= OnDidDeselectAnnotationView;
if (e.NewElement != null)
var formsMap = (CustomMap)e.NewElement;
var nativeMap = Control as MKMapView;
customPins = formsMap.CustomPins;
nativeMap.GetViewForAnnotation = GetViewForAnnotation;
nativeMap.CalloutAccessoryControlTapped += OnCalloutAccessoryControlTapped;
nativeMap.DidSelectAnnotationView += OnDidSelectAnnotationView;
nativeMap.DidDeselectAnnotationView += OnDidDeselectAnnotationView;
错误是:Error CS0407: 'MKAnnotationView CustomMapRenderer.OnDidSelectAnnotationView(object, MKAnnotationViewEventArgs)' has the wrong return type (CS0407) (MaritsaTundzhaForecast.ios)
是否有其他方法可以在点击时更改地图标记图标?
这是我的GetViewForAnnotation
方法:
protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
//от mainpage извикваш метода
MKAnnotationView annotationView = null;
if (annotation is MKUserLocation)
return null;
var customPin = GetCustomPin(annotation as MKPointAnnotation);
//Get Value
c_annotation = annotation;
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.CalloutOffset = new CGPoint(0, 0);
((CustomMKAnnotationView)annotationView).Name = customPin.Name;
((CustomMKAnnotationView)annotationView).Url = customPin.Url;
((CustomMKAnnotationView)annotationView).Address = customPin.Address;
//Add First Line
((CustomMKAnnotationView)annotationView).AlertLevel = customPin.AlertLevel;
if (customPin.AlertLevel == 1)
annotationView.Image = UIImage.FromFile("green.png");
else if (customPin.AlertLevel == 2)
annotationView.Image = UIImage.FromFile("yellow.png");
else if (customPin.AlertLevel == 3)
annotationView.Image = UIImage.FromFile("orange.png");
else if (customPin.AlertLevel == 4)
annotationView.Image = UIImage.FromFile("red.png");
//Add Second Line
((CustomMKAnnotationView)annotationView).CodeNum = customPin.CodeNum;
((CustomMKAnnotationView)annotationView).MapCode = customPin.MapCode;
annotationView.CanShowCallout = true;
configureDetailView(annotationView);
return annotationView;
我想点击放置不同大小的相同图标。
我用这个example
【问题讨论】:
您不能只更改被覆盖方法的返回类型。您尝试做的可能需要比标准地图控件更可定制的东西 当我点击不同大小的标记时,我想更改标记上的图标.. 【参考方案1】:在 DidSelectAnnotationView 中设置 pin 图像,如:
nativeMap.DidSelectAnnotationView += OnDidSelect;
private void OnDidSelect(object sender, MKAnnotationViewEventArgs e)
e.View.Image = UIImage.FromFile("monkey.png");
【讨论】:
谢谢!你能在这里检查我关于更换引脚的问题吗..我有一个小问题..***.com/questions/69620109/…以上是关于如何在单击 xamarin.forms.maps iOS c# 时更改 pin 图标的主要内容,如果未能解决你的问题,请参考以下文章
在运行时使用 Xamarin.Forms.Maps 创建折线时如何附加位置?
Xamarin.Forms.Maps 如何像谷歌地图一样在图钉旁边/上方有一个标签?
在 iOS 上的 Xamarin.Forms.Maps 中使用 Xamarin.Essentials Geolocation 获取设备位置的奇怪问题
从 ViewModel 绑定到 Xamarin.Forms.Maps.Map