我开始使用Silverlight Bing Maps控件。如何使用C#以编程方式将图钉添加到地图中?
谢谢!
答案 0 :(得分:21)
未知,
以下是构建Silverlight应用程序的分步说明,该应用程序显示美国的Bing地图,并在每个单击的位置添加图钉。只是为了好玩,我在浏览图钉时添加了一些“悬停”功能。
第1步:使用Visual Studio(文件/新项目/ Silverlight应用程序)创建示例Silverlight应用程序
第2步:将两个Bing DLL引用添加到Silverlight应用程序项目
Folder: C:\Program Files\Bing Maps Silverlight Control\V1\Libraries\ File 1: Microsoft.Maps.MapControl.dll File 2: Microsoft.Maps.MapControl.Common.dll
第3步:编辑MainPage.xaml,并在顶部添加以下命名空间:
xmlns:Maps="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl"
第4步:编辑MainPage.xaml,并将以下代码放在UserControl的网格中:
<Maps:Map x:Name="x_Map" Center="39.36830,-95.27340" ZoomLevel="4" />
第5步:编辑MainPage.cs,并添加以下using语句:
using Microsoft.Maps.MapControl;
第6步:编辑MainPage.cs,并使用以下代码替换MainPage类:
public partial class MainPage : UserControl
{
private MapLayer m_PushpinLayer;
public MainPage()
{
InitializeComponent();
base.Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
base.Loaded -= OnLoaded;
m_PushpinLayer = new MapLayer();
x_Map.Children.Add(m_PushpinLayer);
x_Map.MouseClick += OnMouseClick;
}
private void AddPushpin(double latitude, double longitude)
{
Pushpin pushpin = new Pushpin();
pushpin.MouseEnter += OnMouseEnter;
pushpin.MouseLeave += OnMouseLeave;
m_PushpinLayer.AddChild(pushpin, new Location(latitude, longitude), PositionOrigin.BottomCenter);
}
private void OnMouseClick(object sender, MapMouseEventArgs e)
{
Point clickLocation = e.ViewportPoint;
Location location = x_Map.ViewportPointToLocation(clickLocation);
AddPushpin(location.Latitude, location.Longitude);
}
private void OnMouseLeave(object sender, MouseEventArgs e)
{
Pushpin pushpin = sender as Pushpin;
// remove the pushpin transform when mouse leaves
pushpin.RenderTransform = null;
}
private void OnMouseEnter(object sender, MouseEventArgs e)
{
Pushpin pushpin = sender as Pushpin;
// scaling will shrink (less than 1) or enlarge (greater than 1) source element
ScaleTransform st = new ScaleTransform();
st.ScaleX = 1.4;
st.ScaleY = 1.4;
// set center of scaling to center of pushpin
st.CenterX = (pushpin as FrameworkElement).Height / 2;
st.CenterY = (pushpin as FrameworkElement).Height / 2;
pushpin.RenderTransform = st;
}
}
第7步:构建并运行!
干杯,Jim McCurdy