using System;

using ESRI.ArcGIS.Client;

using ESRI.ArcGIS.Client.Geometry;

 

namespace CustomLayers

{

    public class GoogleStaticMapLayer : DynamicMapServiceLayer

    {

        private static ESRI.ArcGIS.Client.Projection.WebMercator mercator =

                new ESRI.ArcGIS.Client.Projection.WebMercator();

 

        private const double cornerCoordinate = 20037508.3427892;

        private const int WKID = 102100;

 

        public GoogleStaticMapLayer() : base(){ }

 

        private Lod[] Lods { get; set;}

       

        public override void Initialize()

        {

            this.FullExtent =

            new ESRI.ArcGIS.Client.Geometry.Envelope(-cornerCoordinate, -cornerCoordinate, cornerCoordinate, cornerCoordinate)

            {

                SpatialReference = new SpatialReference(WKID)

            };

 

            this.SpatialReference = new SpatialReference(WKID);

           

            Lods = new Lod[21];

            double resolution = cornerCoordinate * 2 / 256;

            for (int i = 0; i < Lods.Length; i++)

            {

                Lods[i] = new Lod() { Resolution = resolution };

                resolution /= 2;

            }

 

            base.Initialize();

        }

 

        public override void GetUrl(ESRI.ArcGIS.Client.Geometry.Envelope extent, int width, int height,

            DynamicMapServiceLayer.OnUrlComplete onComplete)

        {

            MapPoint geogCenterPoint = null;

            string mapURL = null;           

           

            try

            {

                if (width > 640 || height > 640)

                    throw new Exception("Width or height greater than 640");

               

                double currentResolution = extent.Width / width;

                int currentLodIndex = 0;

                int requestWidth = 0;

                int requestHeight = 0;

                for (int i = 0; i < Lods.Length; i++)

                {

                    Lod lod = Lods[i];

                    currentLodIndex = i;

                    if ((int)lod.Resolution <= (int)currentResolution)

                    {

                        requestWidth = (int)(extent.Width / lod.Resolution);

                        requestHeight = (int)(extent.Height / lod.Resolution);

                        break;

                    }

                }

 

                if (requestWidth > 640 || requestHeight > 640)

                    throw new Exception("Request width or height greater than 640");               

 

                geogCenterPoint = mercator.ToGeographic(extent.GetCenter()) as MapPoint;

 

                mapURL = string.Format

                  ("http://maps.google.com/maps/api/staticmap?center={0},{1}&zoom={2}&size={3}x{4}&maptype=roadmap&sensor=false",

                   geogCenterPoint.Y, geogCenterPoint.X, currentLodIndex, requestWidth, requestHeight);

 

            }

            catch (Exception ex)

            {

                // Message box just for debug purposes

                //System.Windows.MessageBox.Show( ex.Message,"Error: Google Static Map not generated", System.Windows.MessageBoxButton.OK);

                return;

            }

           

            onComplete(mapURL, width, height, new ESRI.ArcGIS.Client.Geometry.Envelope()

            {

                XMin = extent.XMin,

                YMin = extent.YMin,

                XMax = extent.XMax,

                YMax = extent.YMax

            });

        }     

    }

}