C# Xamarin自定义地图Pin渲染器
首先,我想透露一下,我是一名新的开发人员,刚刚开始使用Xamarin和C sharp进行编码 嗯。因此,我正在为我的应用程序中的贴图pin创建一个自定义渲染器。我一直在使用下面的示例代码: 目前,我正在尝试设置pin上的弹出窗口,以便当我单击它们时,它们将进入应用程序中的某个页面,或者可能有一个弹出窗口,提供有关特定pin位置的更多详细信息。目前,当我点击pin弹出窗口时,它会将我带到一个网站 以下是该应用程序的屏幕截图: 我一直在看这段代码,我想知道是否有可能将URL更改为本地页面C# Xamarin自定义地图Pin渲染器,c#,xamarin,xamarin.forms,custom-renderer,C#,Xamarin,Xamarin.forms,Custom Renderer,首先,我想透露一下,我是一名新的开发人员,刚刚开始使用Xamarin和C sharp进行编码 嗯。因此,我正在为我的应用程序中的贴图pin创建一个自定义渲染器。我一直在使用下面的示例代码: 目前,我正在尝试设置pin上的弹出窗口,以便当我单击它们时,它们将进入应用程序中的某个页面,或者可能有一个弹出窗口,提供有关特定pin位置的更多详细信息。目前,当我点击pin弹出窗口时,它会将我带到一个网站 以下是该应用程序的屏幕截图: 我一直在看这段代码,我想知道是否有可能将URL更改为本地页面 In
InitializeComponent();
var position1 = new Position(52.649005, -7.250712);
var position2 = new Position(52.648061, -7.252879);
var position3 = new Position(52.650519, -7.249260);
var position4 = new Position(52.652680, -7.244724);
var position5 = new Position(52.648061, -7.252879);
var position6 = new Position(52.648061, -7.252879);
var pin1 = new CustomPin
{
Type = PinType.Place,
Position = position1,
Label = "Butler House",
Address = "394 Pacific Ave, San Francisco CA",
Id = "test",
Url = "http://xamarin.com/about/"
};
var pin2 = new CustomPin
{
Type = PinType.Place,
Position = position2,
Label = "Talbots Tower",
Address = "394 Pacific Ave, San Francisco CA",
Id = "",
Url = "http://xamarin.com/about/"
};
在custommapreder.cs文件中,我认为我需要修改CustomPin.URL,以便在应用程序中单击并获取本地页面
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.Id.ToString());
if (annotationView == null)
{
annotationView = new CustomMKAnnotationView(annotation, customPin.Id.ToString());
annotationView.Image = UIImage.FromFile("pin-red-10.png"); //pin is set by image not changable by colour attributes
annotationView.CalloutOffset = new CGPoint(0, 0);
annotationView.LeftCalloutAccessoryView = new UIImageView(UIImage.FromFile("Ireland-icon.png"));
annotationView.RightCalloutAccessoryView = UIButton.FromType(UIButtonType.DetailDisclosure);
((CustomMKAnnotationView)annotationView).Id = customPin.Id.ToString();
((CustomMKAnnotationView)annotationView).Url = customPin.Url;
}
annotationView.CanShowCallout = true;
return annotationView;
任何关于这方面的帮助或见解都将不胜感激,因为目前我正在周而复始地尝试许多不同的事情
谢谢
MichaelGetViewForAnnotation方法用于获取MKAnnotationView(即显示Pin详细信息的标注) 您需要的是MKMapView标注的点击事件。将EventHandler添加到Callout AccessoryControlTapped并处理导航逻辑 在所提供的样本中
void OnCalloutAccessoryControlTapped(object sender, MKMapViewAccessoryTappedEventArgs e)
{
// handle navigation here instead of opening the webpage
var customView = e.View as CustomMKAnnotationView;
if (!string.IsNullOrWhiteSpace(customView.Url))
{
UIApplication.SharedApplication.OpenUrl(new Foundation.NSUrl(customView.Url));
}
}