use*_*048 11 silverlight bing-maps
我开始使用Silverlight Bing Maps控件了.如何使用C#以编程方式将图钉添加到地图中?
谢谢!
Jim*_*rdy 21
未知,
以下是构建Silverlight应用程序的逐步帖子,该应用程序显示美国的Bing地图,并在每个单击的位置添加图钉.只是为了好玩,我在浏览图钉时添加了一些"悬停"功能.
步骤1:使用Visual Studio创建示例Silverlight应用程序(文件/新项目/ Silverlight应用程序)
第2步:将两个Bing DLL引用添加到Silverlight应用程序项目
Run Code Online (Sandbox Code Playgroud)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"
Run Code Online (Sandbox Code Playgroud)
步骤4:编辑MainPage.xaml,并将以下代码放在UserControl的Grid中:
<Maps:Map x:Name="x_Map" Center="39.36830,-95.27340" ZoomLevel="4" />
Run Code Online (Sandbox Code Playgroud)
步骤5:编辑MainPage.cs,并添加以下using语句:
using Microsoft.Maps.MapControl;
Run Code Online (Sandbox Code Playgroud)
步骤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;
}
}
Run Code Online (Sandbox Code Playgroud)
第7步:构建并运行!
干杯,Jim McCurdy