如何在 xamarin.forms.maps iOS c# 中单击时更改图钉图标
How to change pin icon on click in xamarin.forms.maps iOS c#
我想在单击图钉时更改图钉图标。
我从 OnDidSelectAnnotationView
中的 GetViewForAnnotation
方法复制了这段代码,但我不知道如何 return 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
在DidSelectAnnotationView中设置pin图,如:
nativeMap.DidSelectAnnotationView += OnDidSelect;
private void OnDidSelect(object sender, MKAnnotationViewEventArgs e)
{
e.View.Image = UIImage.FromFile("monkey.png");
}
我想在单击图钉时更改图钉图标。
我从 OnDidSelectAnnotationView
中的 GetViewForAnnotation
方法复制了这段代码,但我不知道如何 return 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
在DidSelectAnnotationView中设置pin图,如:
nativeMap.DidSelectAnnotationView += OnDidSelect;
private void OnDidSelect(object sender, MKAnnotationViewEventArgs e)
{
e.View.Image = UIImage.FromFile("monkey.png");
}