Traffic Layer for Bing Maps in Silverlight

馋奶兔 提交于 2019-12-08 09:43:42

问题


I have a Bing Maps Silverlight Application and want to display Traffic Information on the Map. It seems to be implemented in the AJAX Version, but not in the Silverlight Version.

So how can I implement a working traffic layer for Silverlight?


回答1:


For everyone who is interested in the solution:

After hours of searching and trying I found the solution here: 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;
    }
}

And then add the Layer to the Map:

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

I hope that helps everybody who wants to add a traffic layer to their Silverlight application.

Greetings.




回答2:


Just to clean things up a bit: this does absolutly the same as Johannes answer:

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));
    }
}

And the map layer:

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


来源:https://stackoverflow.com/questions/12529781/traffic-layer-for-bing-maps-in-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!