Common CustomDataSource
Common_CustomDataSource_CSharp\CustomDataSource_CSharp\TiledMapDataSource_CSharp\VirtualEarthTileUrlGenerator.cs
// Copyright 2007 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
//

using System;
using System.Collections.Generic;
using System.Text;

namespace TiledMapDataSource_CSharp
{
    public class VirtualEarthTileUrlGenerator : ITileUrlGenerator
    {
        #region ITileUrlGenerator Members

        public string GetTileUrl(long column, long row, int level, string defaultUrl, Dictionary<string, bool> layerVisibility)
        {
            return GetUrl(Convert.ToInt32(column), Convert.ToInt32(row), level, layerVisibility);
        }

        #endregion

        private static string TileToQuadKey(int tx, int ty, int zl)
        {
            string quad = "";
            for (int i = zl; i > 0; i--)
            {
                int mask = 1 << (i - 1);
                int cell = 0;
                if ((tx & mask) != 0)
                {
                    cell++;
                }
                if ((ty & mask) != 0)
                {
                    cell += 2;
                }
                quad += cell;
            }
            return quad;
        }

        private string GetUrl(int column, int row, int level, Dictionary<string, bool> layerVisibility)
        {
            string mapType = null;
            string mapExtension = null;

            if (layerVisibility["r"] && layerVisibility["a"])
            {
                mapType = "h";
                mapExtension = ".jpeg";
            }
            else if (layerVisibility["r"])
            {
                mapType = "r";
                mapExtension = ".png";
            }
            else if (layerVisibility["a"])
            {
                mapType = "a";
                mapExtension = ".jpeg";
            }
            else
                return null;

            string quadKey = TileToQuadKey(column, row, level);
            
            string url = String.Concat(new object[] { "http://", mapType, quadKey[quadKey.Length - 1], ".ortho.tiles.virtualearth.net/tiles/", mapType, quadKey, mapExtension, "?g=", 1 });
            return url;
        }
    }
}