Silverlight - Bing地图 - 自定义图钉样式

时间:2010-03-10 05:03:49

标签: silverlight

如何在Bing Maps Silverlight控件上自定义图钉的样式?我查看了此处显示的文档(http://www.microsoft.com/maps/isdk/silverlightbeta/#MapControlInteractiveSdk.Tutorials.TutorialCustomPushpin)。但是,我正在以编程方式添加可变数量的Pushpins。理想情况下,我希望能够设置每个推送的风格,但我不知道如何。

3 个答案:

答案 0 :(得分:6)

您有两种方法可以:

(1)创建任何UIElement以传递到PushPinLayer.AddChild。 AddChild方法将接受任何UIElement,例如本例中的图像:

MapLayer m_PushpinLayer = new MapLayer();
Your_Map.Children.Add(m_PushpinLayer);
Image image = new Image();
image.Source = ResourceFile.GetBitmap("Images/Me.png", From.This);
image.Width = 40;
image.Height = 40;
m_PushpinLayer.AddChild(image,
    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>
            <Ellipse Fill="Green" Width="15" Height="15" />
        </Grid>
    </ControlTemplate>
</ResourceDictionary>

答案 1 :(得分:1)

我会通过创建一个图层然后将我的图钉添加到该图层来完成此操作。

// during initial load
MapLayer lay = new MapLayer();
MapControl.Children.Add(lay);


// for each pushpin you want to add
Image image = new Image();
// this assumes you have an "Images" folder on the root of your host web application
image.Source = new BitmapImage(new Uri(App.Current.Host.Source, "../Images/PushPin.png"));
var lat = 40.4d;
var long = -81.8d;
Location location = new Location(lat, long, 0d);

//Define the image display properties
image.Opacity = 1.0;
image.Stretch = Stretch.None;

// Center the image around the location specified
PositionOrigin position = PositionOrigin.Center;

//Add the image to the defined map layer
lay.AddChild(image, location, position);

答案 2 :(得分:1)

Private Sub MainPage_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    Dim pushpin As Microsoft.Maps.MapControl.Pushpin = New Microsoft.Maps.MapControl.Pushpin
    pushpin.Template = Application.Current.Resources("PushPinTemplate")
End Sub

没有错误......