我正在尝试给我的pushpins添加标签,我正在试验两种不同的方法来将pushpin添加到地图上。
测试1是来自xaml代码,我可以添加pushpin,但我不知道如何添加文本。
测试2来自C#代码,当我试图打开地图时,我得到一个错误的 "Object refernce not set to an instance of an object on the line "myMap.Children.Add(pin);"。
XAML代码。
<Window x:Class="WPFKiosk.MapWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFKiosk"
mc:Ignorable="d"
Title="MapWindow" Height="910" Width="1080" WindowStyle="None" ResizeMode="NoResize">
<!-- -->
<Window.Resources>
<ControlTemplate x:Key="PushpinControlTemplate" TargetType="m:Pushpin">
<Image x:Name="pinImage" Height="64" Source="/Images/Push_Pin.png"/>
</ControlTemplate>
</Window.Resources>
<Grid Width="1080" Height="915">
<m:Map x:Name="myMap" CredentialsProvider="My_Key" Mode="Road">
<m:Pushpin Location="28,-81"/>
<!-- Test 1 -->
</m:Map>
<Image HorizontalAlignment="Left" Height="100" Margin="510,740,0,0" VerticalAlignment="Top" Width="100" Source="Images/iTO Back Arrow.png" MouseLeftButtonDown="Image_MouseLeftButtonDown"/>
</Grid>
</Window>
C#代码:
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using Microsoft.Maps.MapControl.WPF;
using Microsoft.Maps.MapControl.WPF.Design;
using Microsoft.Maps.MapControl.WPF.Core;
using Microsoft.Maps.MapControl.WPF.Overlays;
using System.Windows.Controls;
namespace WPFKiosk
{
/// <summary>
/// Interaction logic for MapWindow.xaml
/// </summary>
public partial class MapWindow : Window
{
private DispatcherTimer closeTimer;
public MapWindow()
{
Pushpin pin = new Pushpin();
pin.Location = new Location(28.5383, -81.3792);
pin.Content = "text";
pin.Template = (ControlTemplate)(this.Resources["PushpinControlTemplate"]);
myMap.Children.Add(pin);
//Test 2
this.Left = 0;
this.Top = 0;
this.Topmost = true;
InitializeComponent();
LocationConverter locConverter = new LocationConverter();
// Setting the map view... aka Zoom level and center of zoom
// A string of the coordinates of a location is required
String OrlandoLoc = "28.5383,-81.3792,0.0";
// The String is then converted to a location that the map can interpret
Location center = (Location)locConverter.ConvertFrom(OrlandoLoc);
myMap.SetView(center, 13);
closeTimer = new DispatcherTimer();
closeTimer.Interval = TimeSpan.FromMinutes(2);
closeTimer.Tick += CloseTimer_Tick;
closeTimer.Start();
}
}
}
Pushpin是一个ContentControl,所以你可以添加任何你喜欢的内容。
<m:Pushpin Location="28,-81" Content="Hello"/>
或者...
<m:Pushpin Location="28,-81">
<TextBlock Text="Hello"/>
</m:Pushpin>
或任何更复杂的内容,如
<m:Pushpin Location="28,-81">
<Image Source="..."/>
</m:Pushpin>