将图钉添加到Bing地图

将图钉添加到Bing地图

本文介绍了Silverlight-通过C#将图钉添加到Bing地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Silverlight Bing Maps控件.如何使用C#以编程方式向地图添加图钉?

I'm getting started with the Silverlight Bing Maps control. How do I add a Pushpin to a map programmatically with C#?

谢谢!

推荐答案

未知,

这里是构建Silverlight应用程序的分步指南,该应用程序显示美国的Bing地图,并在每个单击的位置添加图钉.只是为了好玩,当您浏览图钉时,我添加了一些悬停"功能.

Here is a step-by-step post for building a Silverlight app that displays a Bing map of the US, and adds a pushpin at each clicked location. And just for fun I added some "hover" functionality when you browse over the pushpins.

步骤1 :使用Visual Studio创建一个示例Silverlight应用程序(文件/新建项目/Silverlight应用程序)

Step 1: Create a sample Silverlight application with Visual Studio (File / New Project / Silverlight Application)

第2步:将两个Bing DLL引用添加到Silverlight应用程序项目中

Step 2: Add the two Bing DLL references to the Silverlight application project

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,并在顶部添加以下名称空间:

Step 3: Edit the MainPage.xaml, and add the following namespace at the top:

xmlns:Maps="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl"

第4步:编辑MainPage.xaml,并将以下代码放入UserControl的网格中:

Step 4: Edit the MainPage.xaml, and place the following code inside the UserControl's Grid:

<Maps:Map x:Name="x_Map" Center="39.36830,-95.27340" ZoomLevel="4" />

第5步:编辑MainPage.cs,并添加以下using语句:

Step 5: Edit the MainPage.cs, and add the following using statement:

using Microsoft.Maps.MapControl;

步骤6 :编辑MainPage.cs,并用以下代码替换MainPage类:

Step 6: Edit the MainPage.cs, and replace the MainPage class with the following code:

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)

Cheers, Jim McCurdy

面对面软件 YinYangMoney

这篇关于Silverlight-通过C#将图钉添加到Bing地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 04:36