【问题标题】:MapControl Template10地图控制模板10
【发布时间】:2016-07-23 22:40:38
【问题描述】:

已解决

我解决了这个问题,因为我必须添加一个 MapItemsControl.ItemTemplate。如果没有这个,它只会呈现控件的名称。

所以只需添加以下代码:

<Maps:MapItemsControl x:Name="mainMapItems" ItemsSource="{Binding MapItems}">
 <Maps:MapItemsControl.ItemTemplate>
  <DataTemplate>
   <StackPanel Background="Transparent">
    <TextBlock Maps:MapControl.Location="{Binding Location}" Text="{Binding Title}" Maps:MapControl.NormalizedAnchorPoint="0.5,0.5" FontSize="20" Margin="5"/>
   </StackPanel>
  </DataTemplate>
 </Maps:MapItemsControl.ItemTemplate>
</Maps:MapItemsControl>

这并不完美,因为这不会在地图上为您提供图标,而只是一个文本。但可以通过在 Collection 中添加 Image = "" 轻松解决。


我正在尝试在 Template10 布局中使用 MapControl。

我使用的代码是

MainPage.xaml

            <Maps:MapControl x:Name="MapControl1"
            ZoomInteractionMode="GestureAndControl"
            TiltInteractionMode="GestureAndControl"   
            MapServiceToken="<ApiCodeHere>"
            Loaded="{x:Bind ViewModel.Map_Loaded}"/>

MainPageViewModel.cs

    MapControl _Map;
    public MapControl Map { get { return _Map; } set { Set(ref _Map, value); RaisePropertyChanged(); } }

    public async void Map_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {

        MapControl newMap = new MapControl();

        Geoposition userLocation = await GetUserLocation();

        BasicGeoposition GeoPos_UserLocation = new BasicGeoposition() { Latitude = userLocation.Coordinate.Point.Position.Latitude, Longitude = userLocation.Coordinate.Point.Position.Longitude };
        BasicGeoposition GeoPos_NorthWest = new BasicGeoposition() { Latitude = userLocation.Coordinate.Point.Position.Latitude + 0.05, Longitude = userLocation.Coordinate.Point.Position.Latitude + 0.1 };
        BasicGeoposition GeoPos_SouthEast = new BasicGeoposition() { Latitude = userLocation.Coordinate.Point.Position.Latitude - 0.05, Longitude = userLocation.Coordinate.Point.Position.Latitude - 0.1 };

        GeoboundingBox mapBox = new GeoboundingBox(GeoPos_NorthWest, GeoPos_SouthEast);

        // Add Point for User
        MapIcon Icon_UserLocation = new MapIcon() { Location = new Geopoint(GeoPos_UserLocation) };
        newMap.MapElements.Add(Icon_UserLocation);
        newMap.Center = new Geopoint(mapBox.Center);

        Map = newMap;

        await Task.CompletedTask;
    }

Map_Loaded 函数按预期触发。我遇到的问题是,如果这是一个普通项目,我会将数据直接设置为 MapControl1.Center 和 MapControl1.MapElements.Add。但由于这是一个 MVVM 项目,所以这不是它的完成方式,我对如何进行有点困惑。

我想做一些类似 Views.MainPage.MapControl1.Center = new Geopoint() 的事情,但这显然行不通。


其他更新

事实证明,这和我想象的一样简单。这只是按正确顺序思考的问题。

MapControl 具有缩放和居中等控件。所以对于 MVVM 代码这是可行的

Center="{Binding MapCenter,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Zoom="{Binding MapZoom,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"

但是,我在设置 MapItems 时遇到了问题,如我提供的文档中所述。

要在地图上获取项目,您需要添加 MapItemsControl,它应该像这样工作......

<Maps:MapItemsControl x:Name="mainMapItems" ItemsSource="{Binding MapItems,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></Maps:MapItemsControl>

但我在 MainPageViewModel.xaml 中的代码不适用于此。项目没有更新。

IList<MapElement> _MapItems;
public IList<MapElement> MapItems { get { return _MapItems; } set { Set(ref _MapItems, value); RaisePropertyChanged(); } }

IList<MapElement> MapItems = new List<MapElement>() {
    new MapIcon() {
        Location = new Geopoint(GeoPos_UserLocation),
        Title = "You Are Here!"
    }
};

来源:Windows 10 SDK Bing Maps Control

【问题讨论】:

    标签: c# xaml uwp template10 uwp-maps


    【解决方案1】:

    尝试使用

    ObservableCollection ObserverableCollection<MapElement> MapItems = new ObservableCollection<MapElement>();

    由于您希望项目“在运行时可更新”或对更改做出反应 ObservableCollection 在幕后实现 INPC

    【讨论】:

    • 这并没有什么不同。这些项目仍未显示在地图上。
    【解决方案2】:

    我解决了这个问题,因为我必须添加一个 MapItemsControl.ItemTemplate。如果没有这个,它只会呈现控件的名称。

    所以只需添加以下代码:

    <Maps:MapItemsControl x:Name="mainMapItems" ItemsSource="{Binding MapItems}">
     <Maps:MapItemsControl.ItemTemplate>
      <DataTemplate>
       <StackPanel Background="Transparent">
        <TextBlock Maps:MapControl.Location="{Binding Location}" Text="{Binding Title}" Maps:MapControl.NormalizedAnchorPoint="0.5,0.5" FontSize="20" Margin="5"/>
       </StackPanel>
      </DataTemplate>
     </Maps:MapItemsControl.ItemTemplate>
    </Maps:MapItemsControl>
    

    这并不完美,因为这不会在地图上为您提供图标,而只是一个文本。但可以通过在 Collection 中添加 Image = "" 轻松解决。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      • 2017-08-05
      • 2020-06-15
      • 1970-01-01
      相关资源
      最近更新 更多