문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top