如何设置Bing Map API图层的不透明度,但保留PushPins对象的完全100%不透明度?

时间:2011-05-07 01:01:17

标签: silverlight silverlight-4.0 bing-maps bing-api

我想通过设置不透明度来淡化地图图层。 但是,我想保留在地图上呈现的任何对象(即图钉)没有alpha(没有不透明度)。

任何人都知道这是否可行(API是否以某种方式支持)?

谢谢!

1 个答案:

答案 0 :(得分:1)

我们需要一种方法来设置地图控件用于显示地图本身的内部MapTileLayer的样式。不幸的是,API没有提供这种访问级别。

但是我们可以使用VisualTreeHelper来访问MapTileLayer。我使用此blog中的扩展类来帮助解决这个问题。有了这个课程,我们可以做这样的事情: -

        MapTileLayer tileLayer = myMapControl.Descendents().OfType<MapTileLayer>().FirstOrDefault();
        if (tileLayer != null)
        {
            tileLayer.Opacity = 0.5;  // set the opacity desired.
        }

然而,最好通过创建一个派生自Map的新类并分配样式而不是像Opacity这样的单个属性来正确地完成它。

[StyleTypedProperty(Property = "TileLayerStyle", StyleTargetType = typeof(MapTileLayer))]
public class MapEx : Map
{
    #region public Style TileLayerStyle
    public Style TileLayerStyle
    {
        get { return GetValue(TileLayerStyleProperty) as Style; }
        set { SetValue(TileLayerStyleProperty, value); }
    }

    public static readonly DependencyProperty TileLayerStyleProperty =
        DependencyProperty.Register(
            "TileLayerStyle",
            typeof(Style),
            typeof(MapEx),
            new PropertyMetadata(null, OnTileLayerStylePropertyChanged));

    private static void OnTileLayerStylePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MapEx source = d as MapEx;
        source.SetTileLayerStyle();
    }
    #endregion public Style TileLayerStyle

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        SetTileLayerStyle();
    }

    private void SetTileLayerStyle()
    {
        MapTileLayer tileLayer = this.Descendents().OfType<MapTileLayer>().FirstOrDefault();
        if (tileLayer != null)
        {
            tileLayer.Style = TileLayerStyle;
        }
    }

有了这个衍生物,我们可以这样做: -

<UserControl x:Class="HostBingMaps.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:HostBingMaps"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
    xmlns:m="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl">
  <Grid x:Name="LayoutRoot">
        <Grid.Resources>
            <Style x:Key="TileLayerStyle" TargetType="m:MapTileLayer">
                <Setter Property="Opacity" Value="0.2" />
            </Style>
        </Grid.Resources>
        <local:MapEx Mode="Aerial" TileLayerStyle="{StaticResource TileLayerStyle}" AnimationLevel="UserInput" UseInertia="True"  CredentialsProvider="__creds_here__">
              <!-- Pushpins here --> 
      </local:MapEx>
    </Grid>
</UserControl>

图钉将保持完全不透明,但地图图像本身将逐渐消失。