ArcGIS for WPF下的Google图层

来源:互联网 发布:知君本无邪txt 编辑:程序博客网 时间:2024/06/10 04:09

试了一下原来SilverLight下的方法,发现直接继承TiledMapServiceLayer然后覆盖GetTileUrl方法是不行的,没办法,只好再向上继承自TiledLayer了,需要覆盖GetTileSource方法,也就意味着要自己做下载图片的实现,其中有个关键的地方:

var web = new WebClient {Credentials = CredentialCache.DefaultCredentials};//要设置证书web.Headers.Add(HttpRequestHeader.UserAgent, "Microsoft Internet Explorer");//要设置UserAgent

好了,下面附上cs文件源码:

using System;using System.IO;using System.Net;using System.Windows.Media;using System.Windows.Media.Imaging;using ESRI.ArcGIS.Client;using ESRI.ArcGIS.Client.Geometry;namespace WPF.Map{    public class GoogleMapTileLayer : TiledLayer    {        /// <summary>        /// 切片源点的坐标常量        /// </summary>        private const double OriginX = -20037508.342787;        private const double OriginY = 20037508.342787;        private int _mapType = 0;        private int WKID = 102100;        private string _languageCode = "zh-CN";        private string _countryCode = "cn";        /// <summary>        /// 地图类型 0:地形图 1:卫星图 2:街道图        /// </summary>        public int MapType { get { return _mapType; } set { _mapType = value; } }        public string LanguageCode        {            get { return _languageCode; }            set { _languageCode = value; }        }        public string CountryCode        {            get { return _countryCode; }            set { _countryCode = value; }        }        protected override void GetTileSource(int level, int row, int col, Action<ImageSource> onComplete)        {            string url, BaseUrl;            switch (_mapType)            {                case 0: //地形图                    BaseUrl = "m@210000000";                    url = string.Format("http://mt{0}.google.cn/vt/lyrs={1}&v=w2.114&hl={2}&gl={3}&src=app&x={4}&y={5}&z={6}&s={7}",                                col % 4, BaseUrl, LanguageCode, CountryCode, col, row, level,                                "Galileo".Substring(0, (col * 3 + row) % 8));                    break;                case 1: //卫星图                    BaseUrl = "s@126";                    url = string.Format("http://mt{0}.google.cn/vt/lyrs={1}&v=w2.114&hl={2}&gl={3}&src=app&x={4}&y={5}&z={6}&s={7}",                                col % 4, BaseUrl, LanguageCode, CountryCode, col, row, level,                                "Galileo".Substring(0, (col * 3 + row) % 8));                    break;                case 2: //街道图                    BaseUrl = "h@210000000";                    url = string.Format("http://mt{0}.google.cn/vt/imgtp=png32&lyrs={1}&v=w2.114&hl={2}&gl={3}&src=app&x={4}&y={5}&z={6}&s={7}",                                col % 4, BaseUrl, LanguageCode, CountryCode, col, row, level,                                "Galileo".Substring(0, (col * 3 + row) % 8));                    break;                default: //地形图                    BaseUrl = "m@210000000";                    url = string.Format("http://mt{0}.google.cn/vt/lyrs={1}&v=w2.114&hl={2}&gl={3}&src=app&x={4}&y={5}&z={6}&s={7}",                                col % 4, BaseUrl, LanguageCode, CountryCode, col, row, level,                                "Galileo".Substring(0, (col * 3 + row) % 8));                    break;            }            var web = new WebClient {Credentials = CredentialCache.DefaultCredentials};            web.Headers.Add(HttpRequestHeader.UserAgent, "Microsoft Internet Explorer");            web.DownloadDataCompleted += (sender, args) =>                                             {                                                 if (args.Error == null && onComplete != null)                                                 {                                                     var ms2 = new MemoryStream(args.Result, 0, args.Result.Length);                                                     ms2.Seek(0, SeekOrigin.Begin);                                                     var image = new BitmapImage();                                                     image.BeginInit();                                                     image.CacheOption = BitmapCacheOption.None;                                                     image.CreateOptions = BitmapCreateOptions.None;                                                     image.StreamSource = ms2;                                                     image.EndInit();                                                     onComplete(image);                                                 }                                             };            web.DownloadDataAsync(new Uri(url, UriKind.RelativeOrAbsolute));        }        public override void Initialize()        {            this.FullExtent = new Envelope(-20037508.342787, -20037508.342787, 20037508.342787, 20037508.342787);            {                SpatialReference = new SpatialReference(WKID);            };            this.SpatialReference = new SpatialReference(WKID);            this.TileInfo = new TileInfo()            {                Height = 256,                Width = 256,                Origin = new MapPoint(-20037508.342787, 20037508.342787)                {                    SpatialReference = new SpatialReference(WKID)                },                Lods = new Lod[20]            };            double resolution = 156543.033928;            for (int i = 0; i < TileInfo.Lods.Length; i++)            {                TileInfo.Lods[i] = new Lod() { Resolution = resolution };                resolution /= 2;            }            base.Initialize();        }    }}


1 0
原创粉丝点击