【问题标题】:Xamarin Forms Maps iOS bounds - show all pins on the screenXamarin Forms Maps iOS bounds - 在屏幕上显示所有引脚
【发布时间】:2020-11-29 04:01:09
【问题描述】:

我正在使用 Xamarin Forms Maps,我希望在 iOS 上拥有与在 Android 设备上相同的功能 - 在屏幕上显示所有图钉(适当的位置和缩放)

我看的是“Bounds”属性,但我只在 XF.GoogleMaps 上找到了我不使用的 nuget。

对于 Android,我在我的自定义渲染器中撅嘴 LatLngBounds 类并创建了一个构建器,这工作得很好,但在 iOS 上我没有找到解决方案。

谢谢

更新 #1: 这是 iOS 渲染器中放置 ShowAnnotations 的正确方法吗?以及如何将其从 Pin 模型重新打包到 MkAnnotation 中?

        protected override void OnElementChanged(ElementChangedEventArgs<View> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                var nativeMap = Control as MKMapView;
                nativeMap.GetViewForAnnotation = null;
                nativeMap.ZoomEnabled = true;
               
                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;

                //MKAnnotation an = formsMap.CustomPins;
                var numberOfPins = formsMap.CustomPins.Count;
                MKAnnotation[] anotationArray = new MKAnnotation[numberOfPins];

                for (int i = 0; i < numberOfPins; i++)
                {
                    anotationArray[0].Coordinate = customPins.ElementAt(0).Position...
                }
                foreach (var pin in formsMap.CustomPins)
                {

                }
              
                nativeMap.ZoomEnabled = true;
                nativeMap.GetViewForAnnotation = GetViewForAnnotation;
                nativeMap.CalloutAccessoryControlTapped += OnCalloutAccessoryControlTapped;
                nativeMap.DidSelectAnnotationView += OnDidSelectAnnotationView;
                nativeMap.DidDeselectAnnotationView += OnDidDeselectAnnotationView;
            }
        }

【问题讨论】:

  • 设置区域属性

标签: xamarin xamarin.forms xamarin.ios


【解决方案1】:

您正在寻找MKMapView 上的ShowAnnotations

您传入您希望地图居中并缩放的图钉数组 (annotations) 以使所有这些图钉能够同时显示:

yourMapView.ShowAnnotations (new [] { pin }, false);

苹果文档:showAnnotations:animated:

Xamarin 文档:ShowAnnotations

【讨论】:

  • 谢谢,您能否发布一个解决方案,将 customPin 转换为 MkAnnotation?因为我需要一个用于方法 ShowAnnotation(new MkAnnotation[]..) 的数组,请查看我更新的问题
  • @Stefan0309 在 MKMapView 实例(您的 nativeMap)上有一个 Annotations 属性,您可以使用它来检索所有“自定义引脚”的 IMKAnnotation[]
  • ShowAnnotations 方法不会取消缩放地图以预览所有图钉。我将为这个问题开始赏金,但是我们可以更多地讨论实现代码
猜你喜欢
  • 1970-01-01
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多