无法在视图中显示以下列表中的所有插针. 注:我下面的样本不是xamarin forms documentation个中的一部分.这是我的 idea ,也许我可以将这两个列表结合在一起,并在 map 上显示.

  1. Pin.id==1(主针)常量更改其位置,try 添加不同的图像或 colored颜色
  2. 在加载 map 时,我正在try 显示另外9个别针(静态别针),这些别针不会移动.我再一次指定了不同的图像 colored颜色 . 结果,一个列表将覆盖另一个列表.因此,我要么显示引脚1,要么显示其他9个引脚.但我不能同时把10个插针放在屏幕上.

下面是如何创建第一个静态列表和单个PIN

  public class CustomMap : Map
        {
            public List<CustomPin> AdditionalPins { get; set; }
            public List<CustomPin> CustomPins { get; set; }
           
       
    public CustomMap()
    {
       
        CustomPins = new List<CustomPin>();
        
        AdditionalPins = new List<CustomPin>();
    }

在主页上:两个PIN列表

var customPin = new CustomPin
                                {
                                    PinId = 1,
                                    // Position = new Position(37.7749, -122.4194), // Set the position of the pin
                                    Label = "Custom Pin",
                                    Address = "Custom Address",
                                    Rotation = -45 // Set the rotation angle in degrees
                                };
                                Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
                                {
                                    customPin.Position = pos;
                                    // pins.Position = pos;
        
                                    if (myMap.Pins.Count == 0)
                                    {
                                        
                                        myMap.Pins.Add(customPin);
                                    
                                    }....

2.一组静态引脚

     foreach (var coordinate in coordinates)
                    {
                        var poss = new Position(coordinate.Latitude, coordinate.Longitude);
                        CustomPin additionalPin = new CustomPin
                        {
                            PinId = 20,
                            Label = "Additional Pin",
                            Name = coordinate.StopNumber
                            // Add more properties as needed
                        };
    
                        myMap.AdditionalPins.Add(additionalPin);

现在,我得到了两个列表,一个有一个针脚,每10秒更新一次位置,另一个(adittionalpins=9组坐标) 在这里,将这两个列表结合起来

   private List<CustomPin> GetAllPins(MKPointAnnotation mKPointAnnotation)
        {
            List<CustomPin> matchingPins = new List<CustomPin>();

            if (mKPointAnnotation != null)
            {
                var position = new Position(mKPointAnnotation.Coordinate.Latitude, mKPointAnnotation.Coordinate.Longitude);


                // Check Pins collection
                foreach (var pin in ((CustomMap)Element).Pins)
                {
                   
                        matchingPins.Add(pin as CustomPin);
                        // If a match is found in Pins, no need to check AdditionalPins, break out of the loop
                       
                    
                }

              //  Check AdditionalPins collection only if a match is not found in Pins


                    foreach (var additionalPin in ((CustomMap)Element).AdditionalPins)
                {

                    matchingPins.Add(additionalPin);
                    // If a match is found in AdditionalPins, no need to continue the loop



                }

            }

            return matchingPins;
        }

protected override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
        {
            if (annotation is MKUserLocation)
                return null;
         

            var result = GetAllPins(annotation as MKPointAnnotation); //HERE -- get all the items 
            if (result != null)
            {
                
                List<CustomPin> pins = result;

                if (pins != null && pins.Any())
                {
                    
                    foreach (var D in pins)
                    {
                          if (annotationView == null)
                         {
                         annotationView = new MKAnnotationView(annotation, reuseIdentifier);

                            if (D.PinId == 1) //it gets here the first time 
                            {
                                annotationView.Image = UIImage.FromFile("lilocation.png");
                              
                            }
                            else if (D.PinId == 20) //never assign this part to the view 
                            {
                                annotationView.Image = UIImage.FromFile("red.png");
                            }

                           
                        }
                       
                    }
                }

                    return annotationView;
                
            }

目标是显示列表中的每一项及其图像和行为.问题是,视图只在第一次得到NULL.

推荐答案

我不知道为什么你会得到奇怪的结果,但我更喜欢使用MVVM模式来显示 map 上的Pins集合.

您可以使用数据绑定来显示管脚.你可以参考这个:Display a pin collection.

这是我的小演示.所有需要的数据都在MainPageViewModel中.

<local:CustomMap x:Name="customMap" ItemsSource="{Binding PinsCollection}"
                 MapType="Street">
    <local:CustomMap.ItemTemplate>
        <DataTemplate x:DataType="local:PinData" >
            <local:CustomPin PinId="{Binding PinId}" Name="{Binding PinName}"
                             Label="{Binding PinLabel}"
                             Position="{Binding PinPostion}"/>
        </DataTemplate>
    </local:CustomMap.ItemTemplate>
</local:CustomMap>

因此,您可以为CustomPin添加一些BindableProperties,例如PinID或其他.

public class CustomPin : Pin
{

    public static readonly BindableProperty PinIdProperty =BindableProperty.Create("PinId", typeof(int), typeof(CustomPin), null);

    public int PinId
    {
        get { return (int)GetValue(PinIdProperty); }
        set { SetValue(PinIdProperty, value); }
    }

你的设计和我的设计的一个不同之处在于,我不想为自定义管脚和静态管脚创建单独的集合.我想将它们组合在一起,因为实际上我们可以使用PinID或其他属性来识别它们.

因此,在我们的ViewModel中,我只创建了一个集合,

public class MainPageViewModel
{
    public ObservableCollection<PinData> PinsCollection { get; set; } = new ObservableCollection<PinData>();

    //Add some test data
    public MainPageViewModel()
    {
        PinsCollection.Add(
            new PinData()
            {
                PinId = 0,
                PinName = "CustomPin",
                PinPostion = new Position(37.79752, -122.40183),
                PinLabel = "CustomPin"
            });

        PinsCollection.Add(
            new PinData()
            {
                PinId = 1,
                PinName = "AddtionalPin1",
                PinPostion = new Position(37.80252, -122.40183),
                PinLabel = "AddtionalPin1"
            });
        ...

请注意,这里的PinData是Model类.我为它做了一些属性.而且最好是执行INotifyPropertyChanged.然后,控件可能会因数据更改而更改.

public class PinData : INotifyPropertyChanged
{

    public string PinName { get; set; }

    public int PinId { get; set; }

    public string PinLabel { get; set; }

    public string PinAddress { get; set; }

    private Position pinPostition;
    public Position PinPostion
    {
        get
        {
            return pinPostition;

        }
        set
        {
            pinPostition = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PinPostion)));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

以上内容均为FORMS项目.然后,我们还可以对渲染器进行一些更改.

例如,我们可以根据PinId将不同的图像设置为静态或自定义引脚.

CustomMapRenderer年内

IF(AnnotationView==空) { AnnotationView=new CustomMKAnnotationView(Annotation,CustomPin.Name);

   if (customPin.PinId == 0)
   {
       annotationView.Image = UIImage.FromFile("monkey.png");

   }
   else
   {
       annotationView.Image = UIImage.FromFile("pin.png");
   }

好的,这只是一个小演示来展示我对你的 case 的 idea .这就是效果.您可能会看到四个静态的紫色引脚(未移动).一只小猴子会一次几秒钟地向左下方向移动.这是你想要的效果吗?如果你有任何问题请告诉我.

enter image description here

Ios相关问答推荐

如何在SwiftUI中扩展 colored颜色 超出顶部边缘

不使用iOS 17修改器修改SWIFT用户界面视图

如何解释在SwiftUI视图中观察到持续的数据更改

XcodeCloud生成失败,返回";ci_pre_xcodeBuild.sh是可执行文件,但退出时返回2个退出代码.";

无法下载并安装带有Xcode 15.0的iOS 17.0模拟器运行时

安装新版本 XCode 15.0 后无法运行应用程序 XCode

如何使用 SwiftData 从列表中删除子项目?

当目标位于菜单标签内时,matchedGeometryEffect 的行为很奇怪

SwiftUI withAnimation 在视图出现之前不会启动

react 在 android 上运行的本机应用程序,但在 iOS 上出现问题,详细信息在描述中共享

SwiftUI 切换语句

SwiftUI 我自己的渲染函数的返回类型是什么?

在 Swift 中合并子数组

CocoaPods 找不到 podFirebase/Core的兼容版本 | cloud_firestore, Flutter

以编程方式 Select UITextField 中的所有文本

如何计算 Swift 数组中元素的出现次数?

iOS:如何从 URL 调试新启动应用程序

iOS应用程序每隔一次启动就会崩溃,找不到错误

Xcode 5 和 iOS 7:架构和有效架构

Xcode 6 进程启动失败:try 启动应用程序超时