Silverlight中Bing地图的流量层

时间:2012-09-21 11:38:30

标签: silverlight bing-maps layer traffic

我有Bing Maps Silverlight应用程序,并希望在地图上显示交通信息。 它似乎是在AJAX版本中实现的,但不是在Silverlight版本中实现的。

那么如何为Silverlight实现工作流量层?

2 个答案:

答案 0 :(得分:2)

对于对解决方案感兴趣的每个人:

经过几个小时的搜索和尝试,我在这里找到了解决方案: Custom Rendering in Bing Silverlight Control

public class TrafficTileSource : TileSource
{
    public TrafficTileSource()
        : base(GetAbsoluteUrl("http://t0.tiles.virtualearth.net/tiles/t{0}.png"))
    {

    }

    public override Uri GetUri(int x, int y, int zoomLevel)
    {
        var quadKey = new QuadKey(x, y, zoomLevel);
        return new Uri(String.Format(this.UriFormat, quadKey.Key));
    }

    public static string GetAbsoluteUrl(string strRelativePath)
    {
        if (string.IsNullOrEmpty(strRelativePath))
            return strRelativePath;

        string strFullUrl;
        if (strRelativePath.StartsWith("http:", StringComparison.OrdinalIgnoreCase)
          || strRelativePath.StartsWith("https:", StringComparison.OrdinalIgnoreCase)
          || strRelativePath.StartsWith("file:", StringComparison.OrdinalIgnoreCase)
          )
        {
            //already absolute
            strFullUrl = strRelativePath;
        }
        else
        {
            //relative, need to convert to absolute
            strFullUrl = System.Windows.Application.Current.Host.Source.AbsoluteUri;
            if (strFullUrl.IndexOf("/ClientBin") > 0)
                strFullUrl = strFullUrl.Substring(0, strFullUrl.IndexOf("/ClientBin")) + strRelativePath;
        }
        return strFullUrl;
    }
}

然后将图层添加到地图:

<m:MapTileLayer Visibility="{Binding Path=TrafficVisibility,Converter={StaticResource BoolToVisibilityConverter},Mode=OneWay,UpdateSourceTrigger=PropertyChanged}">
                <m:MapTileLayer.TileSources>
                    <utils:TrafficTileSource />
                </m:MapTileLayer.TileSources>
            </m:MapTileLayer>

我希望能帮助每个想要为其Silverlight应用程序添加流量层的人。

问候。

答案 1 :(得分:2)

只是为了清理一下:这与约翰内斯的回答绝对相同:

public class TrafficTileSource : TileSource
{
    public TrafficTileSource() : base("http://t0.tiles.virtualearth.net/tiles/t{0}.png") { }

    public override Uri GetUri(int x, int y, int zoomLevel)
    {
        QuadKey quadKey = new QuadKey(x, y, zoomLevel);
        return new Uri(String.Format(UriFormat, quadKey.Key));
    }
}

地图图层:

<maps:MapTileLayer>
    <maps:MapTileLayer.TileSources>
        <utils:TrafficTileSource />
    </maps:MapTileLayer.TileSources>
</maps:MapTileLayer>