arcgis 中引入bingmap

来源:互联网 发布:mysql 修改复合主键 编辑:程序博客网 时间:2024/05/18 22:43

前期:有KEY码。。。通过注册获取

(1)加入bingmap,因为是3857坐标系统。Spatial Reference: 102100 (3857) 

102100 WGS_1984_Web_Mercator_Auxiliary_Sphere 。

所以要作为最底图加入即可

(2)叠加tile的类:

 

using ESRI.ArcGIS.Client;

using ESRI.ArcGIS.Client.Geometry;

using System.Windows;

using System;

 

namespace Diligentpig

{

    public class ClientDynamicTileMapServiceLayer : TiledMapServiceLayer

    {

        public bool iscansee = true;

        public static readonly DependencyProperty UrlProperty = DependencyProperty.Register("Url", typeof(string), typeof(ClientDynamicTileMapServiceLayer), new PropertyMetadata(new PropertyChangedCallback(ClientDynamicTileMapServiceLayer.OnUrlPropertyChanged)));

        public string Url

        {

            get { return (string)GetValue(UrlProperty); }

            set { SetValue(UrlProperty, value); }

        }

 

        public static readonly DependencyProperty DisableClientCacheProperty = DependencyProperty.Register("DisableClientCache", typeof(bool), typeof(ClientDynamicTileMapServiceLayer), new PropertyMetadata(new PropertyChangedCallback(ClientDynamicTileMapServiceLayer.OnDisableClientCachePropertyChanged)));

        public bool DisableClientCache

        {

            get { return (bool)GetValue(DisableClientCacheProperty); }

            set { SetValue(DisableClientCacheProperty, value); }

        }

 

        private int[] _visibleLayers;

        /// <summary>

        /// Set the visible layers of DynamicTileMapServiceLayer by an int array which starts from 0.

        /// If sets to NULL, all sublayers will be visible.

        /// </summary>

        public int[] VisibleLayers

        {

            get { return _visibleLayers; }

            set

            {

                _visibleLayers = value;

                if (this.IsInitialized)

                {

                    this.IsInitialized = false;

                    this.InitializationFailure = null;

                    this.Refresh();

                    this.Initialize();

                }

            }

        }

 

 

 

        private const double cornerCoordinate = 20037508.3427892;

        public override void Initialize()

        {

            this.FullExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(-20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892)

            {

                SpatialReference = new SpatialReference(102100)

            };

            // This layer's spatial reference

            this.SpatialReference = new SpatialReference(102100);

            // Set up tile information. Each tile is 256x256px, 19 levels.

            this.TileInfo = new TileInfo()

            {

                Height = 256,

                Width = 256,

                Origin = new MapPoint(-cornerCoordinate, cornerCoordinate) { SpatialReference = new ESRI.ArcGIS.Client.Geometry.SpatialReference(102100) },

                Lods = new Lod[19]

            };

            // Set the resolutions for each level. Each level is half the resolution of the previous one.

            double resolution = cornerCoordinate * 2 / 256;

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

            {

                TileInfo.Lods[i] = 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)

        {

            if (iscansee)

            {

                string baseUrl = @"{0}/export?udpi=96&transparent=true&format=png8&bbox={1}%2C{2}%2C{3}%2C{4}&bboxSR=102100&imageSR=102100&size=256%2C256&f=image";

 

                double cornerCoordinate = 20037508.3427892;//from wkid 3857/102100

                double originResolution = cornerCoordinate * 2 / 256;

                double resolution = originResolution;

                for (int i = 0; i < level; i++)

                {

                    resolution /= 2;

                }

                double xmin, ymin, xmax, ymax;

                //double resolution = 39135.7584820001;

                xmin = -cornerCoordinate + resolution * 256 * col;

                ymin = cornerCoordinate - resolution * 256 * (row + 1);

                xmax = -cornerCoordinate + resolution * 256 * (col + 1);

                ymax = cornerCoordinate - resolution * 256 * row;

 

                //string[] subServices = { "stgeometry", "stgeometry1", "stgeometry2" };

                //string baseUrl = @"http://192.168.200.231:8399/arcgis/rest/services/{0}/MapServer/export?dpi=96&transparent=true&format=png8&bbox={1}%2C{2}%2C{3}%2C{4}&bboxSR=102100&imageSR=102100&size=256%2C256&f=image&_ts={5}";

                //string subservice = subServices[(level + col + row) % subServices.Length];

                //return string.Format(baseUrl, subservice, xmin, ymin, xmax, ymax, DateTime.Now.Ticks.ToString());

 

                if (VisibleLayers != null)//if null, all sublayers are visible.

                {

                    //ex. layers=show:2,4,7

                    string str = "&layers=show:";

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

                    {

                        str = str.Insert(str.Length, VisibleLayers[i].ToString() + ",");

                    };

                    str = str.Remove(str.Length - 1);//remove last ","

                    baseUrl = baseUrl.Insert(baseUrl.Length, str);

                }

                if (DisableClientCache)

                {

                    baseUrl = baseUrl.Insert(baseUrl.Length, "&_ts={5}");

                    return string.Format(baseUrl, Url, xmin, ymin, xmax, ymax, DateTime.Now.Ticks.ToString());

                }

                return string.Format(baseUrl, Url, xmin, ymin, xmax, ymax);

            }

            else return "";

        }

 

        private static void OnUrlPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

        {

            try

            {

                ClientDynamicTileMapServiceLayer layer = (ClientDynamicTileMapServiceLayer)d;

                if (layer.IsInitialized)

                {

                    layer.IsInitialized = false;

                    layer.InitializationFailure = null;

                    layer.Refresh();

                    layer.Initialize();

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show("ArcGISDynamicMapServiceLayer Url is invalid!/n" + ex.Message);

            };            

        }

 

        private static void OnDisableClientCachePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

        {

            ClientDynamicTileMapServiceLayer layer = (ClientDynamicTileMapServiceLayer)d;

            if (layer.IsInitialized)

            {

                layer.IsInitialized = false;

                layer.InitializationFailure = null;

                layer.Refresh();

                layer.Initialize();

            }

        }

    }

}

 

(3)增加坐标转化系统:

 //经纬度转墨卡托

        public static Point lonLat2Mercator(Point lonLat)

        {

            if (type<= 1)

            {

                double x = lonLat.X * 20037508.34 / 180;

                double M_PI = Math.PI;

                double y = Math.Log(Math.Tan((90 + lonLat.Y) * M_PI / 360)) / (M_PI / 180);

                y = y * 20037508.34 / 180;

                Point mercator = new Point(x, y);

                return mercator;

            }

            else

            {

                Point mercator = lonLat;

                return mercator;

            }

        }

 

 

        //墨卡托转经纬度

        public static Point Mercator2lonLat(Point mercator)

        {

 

            if (type<= 1)

            {

                double x = mercator.X / 20037508.34 * 180;

                double y = mercator.Y / 20037508.34 * 180;

                double M_PI = Math.PI;

                y = 180 / M_PI * (2 * Math.Atan(Math.Exp(y * M_PI / 180)) - M_PI / 2);

                Point lonLat = new Point(x, y);

                return lonLat;

            }

            else

            {

                Point lonLat=mercator;

                return lonLat;

            }

        }

 

//================示例如下==========================

 ESRI.ArcGIS.Client.Geometry.Envelope selectedFeatureExtent = feature.Geometry.Extent;

                        double expandPercentage = 100;

                        double widthExpand = selectedFeatureExtent.Width * (expandPercentage / 10);

                        if (widthExpand == 0) widthExpand = 0.6;

                        double heightExpand = selectedFeatureExtent.Height * (expandPercentage / 10);

                        if (heightExpand == 0) heightExpand = 0.6;

                        Point p1 = new Point(selectedFeatureExtent.XMin - (widthExpand / 2),

                        selectedFeatureExtent.YMin - (heightExpand / 2));

                        Point pM1 = ToolConvert.lonLat2Mercator(p1);

                        Point p2 = new Point(selectedFeatureExtent.XMax + (widthExpand / 2),

                        selectedFeatureExtent.YMax + (heightExpand / 2));

                        Point pM2 = ToolConvert.lonLat2Mercator(p2);

                        ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(

                        pM1.X,pM1.Y,

                        pM2.X, pM2.Y);

 

                        mp.MyMap.ZoomTo(displayExtent);

 

原创粉丝点击