Custom TiledMapServiceLayer not working in 2.2 API.

435
1
08-01-2011 01:31 PM
Labels (1)
TorreyStevens
New Contributor
I used the following custom TiledMapServiceLayer in the 2.2 beta API for loading and viewing map tiles from my local disk. This does no longer works in the 2.2 released API. "GetTileUrl" is called as expected and a valid url is returned but the tiles are not displayed. Does anyone have any ideas about what might have changed between the 2.2 beta and the 2.2 release that may cause this behavior?

   public class LocalTileDataLayer : TiledMapServiceLayer
   {
      /// <summary>Simple constant used for full extent and tile origin specific to this projection.</summary>
      private const double cornerCoordinate = 20037508.342789002;

      /// <summary>ESRI Spatial Reference ID for Web Mercator.</summary>
      private const int WKID = 102113; // WGS_1984_Web_Mercator

      private string mBaseURL;

      public LocalTileDataLayer(string aId, double aOpacity, string aBaseURL)
      {
         this.ID = aId;
         this.Opacity = aOpacity;
         mBaseURL = aBaseURL;
      }

      /// <summary>
      /// Initializes the layer.
      /// </summary>
      /// <seealso cref="ESRI.ArcGIS.Client.Layer.Initialized"/>
      /// <seealso cref="ESRI.ArcGIS.Client.Layer.InitializationFailure"/>
      public override void Initialize()
      {
         //Full extent fo the layer
         this.FullExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(-cornerCoordinate, -cornerCoordinate, cornerCoordinate, cornerCoordinate)
         {
            SpatialReference = new SpatialReference(WKID)
         };

         //This layer's spatial reference
         this.SpatialReference = new SpatialReference(WKID);

         //Set up tile information. Each tile is 256x256px, 18 levels.
         this.TileInfo = new TileInfo()
         {
            Height = 256,
            Width = 256,
            Origin = new MapPoint(-cornerCoordinate, cornerCoordinate)
            {
               SpatialReference = new SpatialReference(WKID)
            },
            SpatialReference = new SpatialReference(WKID),
            Lods = new Lod[20]            
         };

         double resolution = cornerCoordinate * 2 / 256;
         for (int i = 0; i < TileInfo.Lods.Length; i++)
         {
            TileInfo.Lods = new Lod() { Resolution = resolution };
            resolution /= 2;
         }

         //Call base initialize to raise the initialization event
         base.Initialize();
      }

      public override string GetTileUrl(int level, int row, int col)
      {
         string url = mBaseURL
                      + "\\L" + ("00" + level.ToString()).Substring(("00" + level.ToString()).Length - 2)
                      + "\\R" + ("00000000" + row.ToString("X")).Substring(("00000000" + row.ToString("X")).Length - 8)
                      + "\\C" + ("00000000" + col.ToString("X")).Substring(("00000000" + col.ToString("X")).Length - 8)
                      + ".png";

         if (System.IO.File.Exists(url))
            return url;

         return "";
      }
   }
0 Kudos
1 Reply
TorreyStevens
New Contributor
I added a "GetTileSource" override and modified the image.CreateOptions from DelayCreation to None. This appears to have fixed my problem but I am not sure of the ramifications. Any input?

      protected override void GetTileSource(int level, int row, int col, Action<ImageSource> onComplete)
      {
         BitmapImage image = new BitmapImage();
         string str = this.GetTileUrl(level, row, col);
         if (!string.IsNullOrEmpty(str))
         {
            image.BeginInit();
            image.CreateOptions = BitmapCreateOptions.None; // was BitmapCreateOptions.DelayCreation
            image.UriSource = new Uri(str, UriKind.Absolute);
            image.EndInit();
            onComplete(image);
         }
         else
         {
            onComplete(null);
         }
      }
0 Kudos