You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 17:31:00