简体   繁体   English

MapControl模板10

[英]MapControl Template10

SOLVED 解决了

I solved this problem with the fact that I have to add a MapItemsControl.ItemTemplate. 我必须添加一个MapItemsControl.ItemTemplate这一事实解决了这个问题。 Without this it does not render anything more than the name of the control. 没有它,它只会呈现控件名称。

So just add this code: 因此,只需添加以下代码:

<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>

It's not perfect because this will not give you an icon on the map, but rather just a text. 这并不完美,因为这不会在地图上为您提供图标,而只会显示文本。 But it can easily be solve with adding Image = "" in the Collection. 但是可以通过在Collection中添加Image =“”来轻松解决。


I'm trying to use MapControl in a Template10 layout. 我正在尝试在Template10布局中使用MapControl。

The code I use is 我使用的代码是

MainPage.xaml MainPage.xaml中

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

MainPageViewModel.cs 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;
    }

The Map_Loaded function is fired as exepcted. Map_Loaded函数作为exepcted触发。 The thing that I have a trouble with is that if this was a normal project I would set the data directly to MapControl1.Center and MapControl1.MapElements.Add. 我遇到的麻烦是,如果这是一个普通项目,则将数据直接设置为MapControl1.Center和MapControl1.MapElements.Add。 But since this is a MVVM project this is not how it's done and I'm a bit confused on how to proceed. 但是,由于这是MVVM项目,所以这不是完成的方式,我对如何继续感到有些困惑。

I would like to do something like Views.MainPage.MapControl1.Center = new Geopoint(), but that clearly does not work. 我想做类似Views.MainPage.MapControl1.Center = new Geopoint()的操作,但是显然不起作用。


Additional Update 附加更新

As it turns out this was as easy as I thought. 事实证明,这和我想的一样容易。 It was just a matter of thinking in the right order. 这只是按正确顺序思考的问题。

The MapControl has controls for Zooming and Center and such. MapControl具有“缩放”和“居中”等控件。 So for MVVM code this works 因此,对于MVVM代码,这可行

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

I have however had issues with setting up MapItems as described in the document I sources to. 但是,如我所获取的文档所述,在设置MapItems时遇到了问题。

To get items on the map you need to add MapItemsControl and it should work like such... 要在地图上获取项目,您需要添加MapItemsControl,它应该像这样...

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

But my code in MainPageViewModel.xaml does not work with this. 但是我在MainPageViewModel.xaml中的代码对此不起作用。 The items does not update. 这些项目不会更新。

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!"
    }
};

Sources: Windows 10 SDK Bing Maps Control 来源: Windows 10 SDK必应地图控件

I solved this problem with the fact that I have to add a MapItemsControl.ItemTemplate. 我必须添加一个MapItemsControl.ItemTemplate这一事实解决了这个问题。 Without this it does not render anything more than the name of the control. 没有它,它只会呈现控件名称。

So just add this code: 因此,只需添加以下代码:

<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>

It's not perfect because this will not give you an icon on the map, but rather just a text. 这并不完美,因为这不会在地图上为您提供图标,而只会显示文本。 But it can easily be solve with adding Image = "" in the Collection. 但是可以通过在Collection中添加Image =“”来轻松解决。

Try using 尝试使用

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

Since you expecting the item to be "updatable at runtime" or react to changes ObservableCollection behind the scenes implements INPC 由于您希望该项目“在运行时可更新”或对幕后的ObservableCollection更改做出反应,因此实现了INPC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM