如何在Xamarin Form googlemaps中默认显示所有图钉的信息窗口

时间:2019-01-23 15:47:53

标签: google-maps xamarin xamarin.forms

我用xamarin形式的googlemaps包渲染了google地图和地图中的图钉。我想默认显示所有引脚的标题。 我尝试过

map.SelectedPin = pinname

,但仅适用于一个引脚。 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

您必须扩展Map类,以创建Pins的可绑定列表。

public class BindableMap : Map {
    public static readonly BindableProperty MapPinsProperty = BindableProperty.Create( 
             nameof(Pins), 
             typeof(ObservableCollection<Pin>),
             typeof(BindableMap), 
             new ObservableCollection<Pin>(),
             propertyChanged: (b, o, n) =>
             { 
                 var bindable = (BindableMap)b; 
                 bindable.Pins.Clear();

                 var collection = (ObservableCollection<Pin>)n; 
                 foreach (var item in collection) 
                     bindable.Pins.Add(item); 
                     collection.CollectionChanged += (sender, e) => 
                     { 
                         Device.BeginInvokeOnMainThread(() => 
                         { 
                             switch (e.Action) 
                             { 
                                 case NotifyCollectionChangedAction.Add: 
                                 case NotifyCollectionChangedAction.Replace: 
                                 case NotifyCollectionChangedAction.Remove: 
                                     if (e.OldItems != null) 
                                         foreach (var item in e.OldItems) 
                                             bindable.Pins.Remove((Pin)item);
                                     if (e.NewItems != null)
                                         foreach (var item in e.NewItems) 
                                             bindable.Pins.Add((Pin)item); 
                                     break;
                                 case NotifyCollectionChangedAction.Reset: 
                                     bindable.Pins.Clear(); 
                                     break; 
                             } 
                         }); 
                     };
             });
    public IList<Pin> MapPins { get; set; }

}

然后,在您的ViewModel中

private ObservableCollection<Pin> _pinCollection = new ObservableCollection<Pin>(); 
public ObservableCollection<Pin> PinCollection { get { return _pinCollection; } set { _pinCollection = value; OnPropertyChanged(); } }

要添加简单的图钉,

PinCollection.Add(new Pin() { Position = YourPosition, Type = PinType.Generic, Label ="ABCD" });

您现在可以使用此XAML轻松引用此控件。

<local:BindableMap MapType="Street" MapPins="{Binding PinCollection}" />