Xamarin自定义地图图钉渲染器

时间:2018-08-07 19:08:49

标签: c# xamarin xamarin.forms custom-renderer

首先,我想透露我是一名新开发人员,刚开始使用Xamarin和C Sharp进行编码。

好的。因此,我正在为应用程序中的地图图钉创建自定义渲染器。我一直在使用下面的示例代码:https://github.com/xamarin/xamarin-forms-samples/tree/master/CustomRenderers/Map/Pin

目前,我正在尝试在图钉上设置弹出窗口,以便单击它们时,它们将转到应用程序中的页面,或者可能会弹出一个窗口,提供有关该特定图钉位置的更多详细信息。当前,当我单击图钉弹出窗口时,它将带我到网站。

以下是该应用的屏幕截图: enter image description here

我一直在看这段代码,我正试图找出是否可以将URL更改为本地页面?

  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/"

        };

我认为,在CustomMapRender.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;

对此我的任何帮助或见解都将受到赞赏,因为我目前正在圈子里尝试许多不同的事情。

谢谢 迈克尔

1 个答案:

答案 0 :(得分:0)

GetViewForAnnotation方法用于获取 MKAnnotationView (即显示Pin细节的标注)。

您需要的是MKMapView标注的tap事件。将EventHandler添加到 CalloutAccessoryControlTapped 并处理导航逻辑。

在提供的示例中为this part

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));
            }
        }