A sample custom title source of Silverlight Bing map

2010-09-27


In Silverlight web page:

<m:Map>
    <m:Map.Mode>
        <mCore:MercatorMode/>
    </m:Map.Mode>
    <m:Map.Children>
        <m:MapTileLayer>
                <m:MapTileLayer.TileSources>
                        <local:CustomTileSource/>
                </m:MapTileLayer.TileSources>
        </m:MapTileLayer>
    </m:Map.Children>
</m:Map>

In the code-behind .cs file:

public class CustomTileSource : TileSource
{
        public CustomTileSource()
                : base(GetAbsoluteUrl("/ClientBin/Resources/{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;
        }
}

The code is from here