【问题标题】:showing multiple pushpins on a map in windows phone using mvvm model使用 mvvm 模型在 windows phone 中的地图上显示多个图钉
【发布时间】:2013-11-07 08:37:48
【问题描述】:

我在基于 mvvm 模型的应用程序中有以下视图,它应该显示我使用视图模型中的绑定属性“PushPinLocation”绑定到它的所有图钉。

<MapNS:Map 
    Center="{Binding MapCenter, Mode=TwoWay}" Margin="0,0,-5,0" 
    CartographicMode="{Binding MapMode, Mode=TwoWay}" 
    LandmarksEnabled="True" PedestrianFeaturesEnabled="True"  
    ZoomLevel="{Binding MapZoomLevel, Mode=TwoWay}" 
    Foreground="AliceBlue" Grid.Row="1" Height="713"  Width="425" 
    x:Name="mapPanoramaAddress"  >

    <!--Adding Location to show map initially until the data arrived-->
    <maptk:MapExtensions.Children>

        <maptk:MapItemsControl Name="StoresMapItemsControl" >
            <maptk:MapItemsControl.ItemTemplate>
                <DataTemplate>
                     <maptk:Pushpin x:Name="PushPins" Background="White" 
                        GeoCoordinate="{Binding PushPinLocation}" 
                        Content="{Binding PushPinDisplayText}" 
                        Visibility="Visible" />
                </DataTemplate>
            </maptk:MapItemsControl.ItemTemplate>
        </maptk:MapItemsControl>                                
        <maptk:UserLocationMarker GeoCoordinate="{Binding PushPinLocation}" x:Name="UserLocationMarker" Visibility="Visible" />

    </maptk:MapExtensions.Children>

</MapNS:Map>

在每隔几米触发的 geolocator positionchanged 事件中,我正在设置绑定属性“PushPinLocation”的值(来自我的视图模型),这对于图钉和位置标记很常见。

//PushPinLocation
private GeoCoordinate _PushPinLocation = new GeoCoordinate(40.712923, -74.013292);    //cannot assign null 
public GeoCoordinate PushPinLocation
{
    get { return _PushPinLocation; }
    set
    {
        if (_PushPinLocation != value)
        {
            _PushPinLocation = value;
            RaisePropertyChanged("PushPinLocation");
        }
    }
}

在同一视图模型 geolocator_Position 更改事件中,我正在设置图钉位置:

private void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
    this.PushPinLocation = args.Position.Coordinate.ToGeoCoordinate();
}

但我总是看到最新的显示在地图上,而旧的从不在地图上显示。有什么办法可以保留旧的。

【问题讨论】:

  • 您是绑定到特定点还是绑定到点集合?
  • 到具有绑定属性“PushPinLocation”的特定点。例如this.PushPinLocation = new Geocoordinate(高度,经度,纬度)
  • 如果您想在地图上显示多个点,您(显然)需要绑定到一组点。当您更改模型的属性(PushPinLocation..)时,它会更新视图,到目前为止是正确的。但是期望它只是添加一个新的子元素是错误的假设。如果您想要这种行为,您需要在数据源中有一个位置集合并绑定到该集合,以便所有位置显示在您的地图上。

标签: c# windows-phone-7 mvvm windows-phone-8 windows-phone


【解决方案1】:

这篇文章已经有一年了,但没有得到回复,所以这是我的答案:

不要绑定到单个PushPinLocation,而是使用集合。在您的 ViewModel 中,添加以下内容:

private List<GeoCoordinate> _pushPinLocations;
public List<GeoCoordinate> PushPinLocations
{
    get { return _pushPinLocations; }
    set
    {
        _pushPinLocations = value;
        OnPropertyChanged("PushPinLocations");
    }
}

并将您的活动更改为:

private void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
    this.PushPinLocations.Add(args.Position.Coordinate.ToGeoCoordinate());
}

这会将新位置添加到列表中,只要它绑定到此位置列表,所有图钉都会显示。

<maptk:MapItemsControl Name="StoresMapItemsControl" >
    <maptk:MapItemsControl.ItemTemplate>
        <DataTemplate>
             <maptk:Pushpin x:Name="PushPins" Background="White" 
                GeoCoordinate="{Binding PushPinLocations}" 
                Content="{Binding PushPinDisplayText}" 
                Visibility="Visible" />
        </DataTemplate>
    </maptk:MapItemsControl.ItemTemplate>
</maptk:MapItemsControl>             

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    相关资源
    最近更新 更多