ArcGIS for WPF下的Google图层

7 篇文章 1 订阅
6 篇文章 0 订阅

试了一下原来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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值