TorbenK/TK.CustomMap Xamarin Forms PIN不显示问题求助
TK.CustomMap在Xamarin.Forms中PIN标记无法显示
在Xamarin.Forms项目中使用TorbenK/TK.CustomMap时,PIN标记完全无法显示。已尝试直接复制GitHub源码中的示例代码,但问题依旧。调试确认添加的PIN属性均正常,但无法在视图中渲染,怀疑绑定环节存在问题。
以下是相关代码:
ViewModel
/// <summary> /// Pins bound to the <see cref="TKCustomMap"/> /// </summary> public ObservableCollection<TKCustomMapPin> Pins { get { return _pins; } set { if (_pins != value) { _pins = value; OnPropertyChanged("Pins"); } } } MapSpan _mapRegion = MapSpan.FromCenterAndRadius(new Position(37.787332, -122.410540), Distance.FromKilometers(2)); Position _mapCenter; ObservableCollection<TKCustomMapPin> _pins;
构造函数
_mapCenter = new Position(37.787332, -122.410540); _pins = new ObservableCollection<TKCustomMapPin>(); CenterMap(); void CenterMap() { var stopOne = new Position(26.0769102268052, -80.2535091197863); var pin = new TKCustomMapPin { Position = stopOne, ShowCallout = true, IsDraggable = true, DefaultPinColor = Color.GreenYellow }; _pins.Add(pin); }
XAML
<tk:TKCustomMap x:Name="mapView" IsRegionChangeAnimated ="true" HasZoomEnabled="True" MapType="Satellite" WidthRequest="500" HeightRequest="800" IsShowingUser="true" IsClusteringEnabled="true" MapRegion="{Binding MapRegion}" Polygons="{Binding Polygons}" CalloutClickedCommand ="{Binding CalloutClickedCommand}" Routes="{Binding Routes}" Pins="{Binding Pins}"/>
解决方法
- 检查绑定上下文:确保页面的
BindingContext已正确设置为ViewModel实例,比如在页面构造函数中添加BindingContext = new YourViewModel();。 - 确认PIN在可视区域:你设置的初始地图区域和PIN位置相距极远,PIN可能不在当前显示范围内。可以修改
MapRegion为PIN所在区域,或者在添加PIN后将地图定位到该位置:void CenterMap() { var stopOne = new Position(26.0769102268052, -80.2535091197863); var pin = new TKCustomMapPin { Position = stopOne, ShowCallout = true, IsDraggable = true, DefaultPinColor = Color.GreenYellow }; _pins.Add(pin); MapRegion = MapSpan.FromCenterAndRadius(stopOne, Distance.FromKilometers(2)); } - 关闭聚类测试:XAML中开启了聚类
IsClusteringEnabled="true",如果缩放级别导致PIN被聚类但聚类标记未显示,会造成PIN“消失”。先将该属性设为false测试是否能看到PIN。 - 修正属性赋值方式:构造函数中直接给
_pins赋值不会触发OnPropertyChanged,应改为给Pins属性赋值:// 构造函数中替换原有赋值 Pins = new ObservableCollection<TKCustomMapPin>();
内容的提问来源于stack exchange,提问作者Pxaml
相关产品推荐
相关产品推荐

