【问题标题】:Silverlight - Adding Text to Pushpin in Bing Maps via C#Silverlight - 通过 C# 在 Bing 地图中将文本添加到图钉
【发布时间】:2010-05-10 18:29:44
【问题描述】:

我能够让我的 silverlight Bing 地图接受 Mousclick 并将它们转换为 C# 中的图钉。现在我想在 PushPin 旁边显示一个文本作为当鼠标移过 pin 时出现的描述,我不知道该怎么做。有什么方法可以让我做这件事?

这是 C# 代码:

public partial class MainPage : UserControl

{ 私有 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;
}

}

【问题讨论】:

    标签: c# .net silverlight bing-maps bing


    【解决方案1】:

    你有两条路可以走:

    (1) 创建任何 UIElement 以传递给 PushPinLayer.AddChild。 AddChild 方法将接受任何 UIElement,例如这个包含一个 Image 和一个 TextBlock 的 Grid:

    MapLayer m_PushpinLayer = new MapLayer(); 
    Your_Map.Children.Add(m_PushpinLayer);
    Image image = new Image(); 
    image.Source = ResourceFile.GetBitmap("Images/Pushpin.png", From.This); 
    TextBlock textBlock = new TextBlock();
    textBlock.Text = "My Pushpin";
    Grid grid = new Grid();
    grid.Children.Add(image);
    grid.Children.Add(textBlock);
    
    m_PushpinLayer.AddChild(grid, 
        new Microsoft.Maps.MapControl.Location(42.658, -71.137),   
            PositionOrigin.Center);
    

    (2) 创建一个原生 PushPin 对象以传递给 PushpinLayer.AddChild,但首先设置它的 Template 属性。请注意,PushPin 是 ContentControls,并且具有可以从 XAML 中定义的资源设置的 Template 属性:

    MapLayer m_PushpinLayer = new MapLayer(); 
    Your_Map.Children.Add(m_PushpinLayer); 
    Pushpin pushpin = new Pushpin(); 
    pushpin.Template = Application.Current.Resources["PushPinTemplate"]   
        as (ControlTemplate); 
    m_PushpinLayer.AddChild(pushpin, 
        new Microsoft.Maps.MapControl.Location(42.658, -71.137),   
            PositionOrigin.Center);
    

    ...

    <ResourceDictionary 
        xmlns="http://schemas.microsoft.com/client/2007" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    > 
        <ControlTemplate x:Key="PushPinTemplate"> 
            <Grid> 
                <Image Source="Images/Pushpin.png" /> 
                <TextBlock Text="My Pushpin" /> 
            </Grid> 
        </ControlTemplate> 
    </ResourceDictionary>
    

    祝你好运, 吉姆·麦柯迪

    Face To Face 软件和YinYangMoney

    【讨论】:

    • 谢谢 Jim .. 这很有帮助 :) 起初它没有用 .. 直到我更改了 grid.Add to grid.Children.Add ... 再次感谢!
    猜你喜欢
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多