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

发布于 2024-11-05 22:17:02 字数 118 浏览 0 评论 0原文

我想通过设置不透明度来淡化航空地图图层。 但是,我想为渲染在地图顶部的任何对象(即图钉)保留任何 alpha(无不透明度)。

有人知道这是否可行(API 是否以某种方式支持这一点)?

谢谢!

I'd like to fade the Aerial map layer by setting the opacity.
BUT, i'd like to retain no alpha (no opacity) for any objects (i.e. pushpins) that are rendered on top of the map.

Anyone know if this is feasible (does API support this in some way)?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗琴音 2024-11-12 22:17:02

我们需要的是一种对地图控件用来显示地图本身的内部 MapTileLayer 进行样式设置的方法。不幸的是,API 不提供该级别的访问权限。

不过,我们可以使用 VisualTreeHelper 来访问 MapTileLayer。我在此 博客 中使用扩展类对此提供帮助。使用项目中存在的此类,我们可以做一些像这样的笨拙的事情:-

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

但是,最好通过创建一个从 Map 派生的新类并分配一个样式而不是单个属性来正确地做到这一点不透明度

[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>

图钉将保持完全不透明,但地图图像本身将淡出。

What is needed is a way to style the internal MapTileLayer that the Map control uses to display the map itself. Unfortunately the API does not provide that level of access.

However we can use the VisualTreeHelper to gain access to the MapTileLayer. I use the extensions class in this blog to help with that. With this class present in the project we could do something cludgy like this:-

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

However its probably best to do it properly by creating a new class that derives from Map and assigning a style instead of a single property like 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;
        }
    }

With this derivative in place we can do this:-

<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>

The pushpins will remain fully opaque but the map image itself will be faded out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文