【问题标题】:Xamarin Custom Map Pin RenderersXamarin 自定义地图引脚渲染器
【发布时间】:2019-01-14 23:02:16
【问题描述】:

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

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

就目前而言,我正在尝试在引脚上设置弹出窗口,以便当我单击它们时,它们将转到应用程序中的页面,或者可能会弹出一个弹出窗口,其中包含有关该特定引脚位置的更多详细信息。目前,当我单击 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;

任何帮助或对此的见解将不胜感激,因为我目前正在兜圈子尝试这么多不同的事情。

谢谢 迈克尔

【问题讨论】:

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


    【解决方案1】:

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

    您需要的是 MKMapView 标注的点击事件。将 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));
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 2021-04-04
      • 2018-07-29
      • 1970-01-01
      相关资源
      最近更新 更多