【发布时间】:2020-05-19 19:00:44
【问题描述】:
我正在尝试向我的图钉添加标签,并且我正在尝试两种不同的方法来将图钉添加到地图。
测试 1 来自 xaml 代码,我可以添加图钉,但我不知道如何添加文本
测试 2 来自 C# 代码,当我尝试打开地图时,我收到错误消息“对象引用未设置为“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();
}
}
}
【问题讨论】:
-
您的
myMap变量是在调用InitialiseComponent时分配的。在InitialiseComponent之后移动myMap.Children.Add(pin);这一行 -
谢谢!当然就是这么简单。