原文:ArcGIS API for Silverlight中加载Google地形图(瓦片图)

在做水利、气象、土地等行业中,若能使用到Google的地形图那是再合适不过了,下面就介绍如何在ArcGIS API for Silverlight中加载Google地

形图。先上一个图,初步制作,待后续继续改进

       ArcGIS API for Silverlight 中的ArcGISTiledMapServiceLayer图层,继承自TiledMapServiceLayer。如果想实现自己的缓存地图图
 
层,继承它并重载GetTileUrl方法就可以了。ArcGIS API for Silverlight 内部会计算当前访问的缩放等级level,切片二维编号row,
 
col。这些参数暴露给GetTileUrl方法,在这个方法里面设置访问google静态地图的Url地址。
 
1、在Silverlight项目中新建一个文件夹,并添加一个名为GoogleTopographicLayer.cs文件,内容如下:
 
  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;
  11. using ESRI.ArcGIS.Client;
  12. using ESRI.ArcGIS.Client.Geometry;
  13.  
  14. namespace GoogleMap.CommonClass
  15. {
  16. public class GoogleTopographicLayer: TiledMapServiceLayer
  17. {
  18. private const double cornerCoordinate = 20037508.3427892;
  19. private string _baseURL = "t@128"; //google地形图
  20.  
  21. public override void Initialize()
  22. {
  23. ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();
  24. this.FullExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(-20037508.3427892, -20037508.3427892, 20037508.3427892, 20037508.3427892)
  25. {
  26. SpatialReference = new SpatialReference(102100)
  27. };
  28. //图层的空间坐标系
  29. this.SpatialReference = new SpatialReference(102100);
  30. // 建立切片信息,每个切片大小256*256px,共16级.
  31. this.TileInfo = new TileInfo()
  32. {
  33. Height = 256,
  34. Width = 256,
  35. Origin = new MapPoint(-cornerCoordinate, cornerCoordinate) { SpatialReference = new ESRI.ArcGIS.Client.Geometry.SpatialReference(102100) },
  36. Lods = new Lod[16]
  37. };
  38. //为每级建立方案,每一级是前一级别的一半.
  39. double resolution = cornerCoordinate * 2 / 256;
  40. for (int i = 0; i < TileInfo.Lods.Length; i++)
  41. {
  42. TileInfo.Lods[i] = new Lod() { Resolution = resolution };
  43. resolution /= 2;
  44. }
  45. // 调用初始化函数
  46. base.Initialize();
  47. }
  48.  
  49. public override string GetTileUrl(int level, int row, int col)
  50. {
  51. string url = "http://mt" + (col % 4) + ".google.cn/vt/lyrs=" + _baseURL + "&v=w2.114&hl=zh-CN&gl=cn&" + "x=" + col + "&" + "y=" + row + "&" + "z=" + level + "&s=Galil";
  52. if (_baseURL == "s@92")
  53. {
  54. url = "http://mt" + (col % 4) + ".google.cn/vt/lyrs=" + _baseURL + "&v=w2.114&hl=zh-CN&gl=cn&" + "x=" + col + "&" + "y=" + row + "&" + "z=" + level + "&s=Galil"; //加载Google遥感图
  55. }
  56. if (_baseURL == "t@128")
  57. {
  58. url = "http://mt" + (col % 4) + ".google.cn/vt/lyrs=" + _baseURL + ",r@169000000&v=w2.114&hl=zh-CN&gl=cn&" + "x=" + col + "&" + "y=" + row + "&" + "z=" + level + "&s=Galil";//加载Google地形图
  59. }
  60. if (_baseURL == "m@161000000")
  61. {
  62. url = "http://mt" + (col % 4) + ".google.cn/vt/lyrs=" + _baseURL + "&v=w2.114&hl=zh-CN&gl=cn&" + "x=" + col + "&" + "y=" + row + "&" + "z=" + level + "&s=Galil"; //加载Google街道图
  63. }
  64. return string.Format(url);
  65.  
  66. //调用加载初始的Google街道地图
  67. //string baseUrl = "http://mt2.google.cn/vt/v=w2.116&hl=zh-CN&gl=cn&x={0}&y={1}&z={2}&s=G";
  68. //return string.Format(baseUrl, col, row, level);
  69. }
  70. }
  71. }
  72.  
  73. //以上显示的Google地图类型,有三种,根据需要,可以修改变量_baseURL的初始值即可。

 2、Silverlight项目中的MainPage.xaml文件内容如下:
 
  1. <UserControl x:Class="GoogleMap.MainPage"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. mc:Ignorable="d"
  7. xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
  8. xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
  9. xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
  10. xmlns:layer="clr-namespace:GoogleMap.CommonClass"
  11. d:DesignHeight="300" d:DesignWidth="400" Loaded="UserControl_Loaded">
  12.  
  13. <Grid x:Name="LayoutRoot" Background="White">
  14. <esri:Map x:Name="myMap" IsLogoVisible="False" ZoomDuration="0:00:02" PanDuration="0:00:02">
  15. </esri:Map>
  16. </Grid>
  17. </UserControl>

3、Silverlight项目中的MainPage.xaml.cs文件内容如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using TestDZX.CommonClass;
  13. using ESRI.ArcGIS.Client.Geometry;
  14. using ESRI.ArcGIS.Client;
  15.  
  16. namespace GoogleMap
  17. {
  18. public partial class MainPage: UserControl
  19. {
  20. ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();
  21.  
  22. public MainPage()
  23. {
  24. InitializeComponent();
  25. }
  26.  
  27. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  28. {
  29. GoogleTopographicLayerlayer = new GoogleTopographicLayer();
  30. myMap.Layers.Add(layer);
  31. }
  32. }
  33. }

4、完成以上步骤后,运行,可以看见Google地形图了,just try it,have fun!

5、补充:这里使用的是墨卡托坐标,而我们经常使用的是经纬度坐标,这就需要做转换,下面提供一个类,WKIDConvert.cs,内容

如下:

  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;
  11. using ESRI.ArcGIS.Client.Geometry;
  12.  
  13. namespace GoogleMap.CommonClass
  14. {
  15. public static class WKIDConvert
  16. {
  17. //经纬度转墨卡托
  18. public static MapPoint lonlat2mercator(MapPoint lonlat)
  19. {
  20. MapPoint mercator = new MapPoint();
  21. double X = lonlat.X * 20037508.34 / 180;
  22. double Y = Math.Log(Math.Tan((90 + lonlat.Y) * Math.PI / 360)) / (Math.PI / 180);
  23. Y = Y * 20037508.34 / 180;
  24. mercator.X = X;
  25. mercator.Y = Y;
  26. return mercator;
  27. }
  28.  
  29. //墨卡托转经纬度
  30. public static MapPoint mercator2lonlat(MapPoint mercator)
  31. {
  32. MapPoint lonlat = new MapPoint();
  33. double X = mercator.X / 20037508.34 * 180;
  34. double Y = mercator.Y / 20037508.34 * 180;
  35. Y = 180 / Math.PI * (2 * Math.Atan(Math.Exp(Y * Math.PI / 180)) - Math.PI / 2);
  36. lonlat.X = X;
  37. lonlat.Y = Y;
  38. return lonlat;
  39. }
  40. }
  41. }

然后我们如果要定位到某个城市的话,比如黄山市,其Extent为:117.647738815324,29.4704217183843,118.446182957997,30.4124245048916

我们这里为MainPage.xaml.cs中添加一行代码即可定位到黄山市范围

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{

GoogleTopographicLayer   layer = new GoogleTopographicLayer();

myMap.Layers.Add(layer);
            myMap.Extent = new Envelope(WKIDConvert.lonlat2mercator(new MapPoint(117.647738815324, 29.4704217183843)), WKIDConvert.lonlat2mercator(new MapPoint(118.446182957997, 30.4124245048916)));
}

6、再次补充:在之前的Google地形图上叠加自定义ArcMap地图,可以是动态图也可以是静态图,只要加上发布的地图,即可显示,在MainPage.xaml.cs中添加如下几行代码即可。

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
            //叠加Google地形图瓦片
            GoogleTopographicLayer layer = new GoogleTopographicLayer();
            myMap.Layers.Add(layer);
            layer.Opacity = 1;

//加载动态图
            ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer dLayer = new ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer();
            myMap.Layers.LayersInitialized += (evtsender, args) =>
            {
                myMap.ZoomTo(dLayer.InitialExtent);
            };
            dLayer.Url = "http://localhost/arcgis/rest/services/HS/MapServer/";
            myMap.Layers.Add(dLayer);
            dLayer.Opacity = 1;

myMap.Extent = new Envelope(WKIDConvert.lonlat2mercator(new MapPoint(117.647738815324, 29.4704217183843)), WKIDConvert.lonlat2mercator(new MapPoint(118.446182957997, 30.4124245048916)));
}

效果图如下,这里只是做了黄山市的市界边线,主要作用就是在Google地形图上明显看出黄山市的范围:

===========================================================================

如果觉得对您有帮助,微信扫一扫支持一下:

ArcGIS API for Silverlight中加载Google地形图(瓦片图)的更多相关文章

  1. ArcGIS API for Silverlight地图加载众多点时,使用Clusterer解决重叠问题

    原文:ArcGIS API for Silverlight地图加载众多点时,使用Clusterer解决重叠问题 问题:如果在地图上加载成百上千工程点时,会密密麻麻,外观不是很好看,怎么破? 解决方法: ...

  2. arcgis api for JavaScript _加载三维图层(scene layer)

    arcgis api for JavaScript _加载三维图层(scene layer) arcgis api for JavaScript  4.x 版本增加对三维的支持. 关于三维图层(sce ...

  3. ArcGis API for JavaScript学习——加载地图

    ArcGis API for JavaScript开发笔记——加载地图 在这个例子中使用的离线部署的API(请参见 http://note.youdao.com/noteshare?id=f42865 ...

  4. ArcGIS API for Silverlight中专题地图的实现浅析

    原文http://www.gisall.com/html/32/7232-2418.html 专题地图是突出表现特定主题或者属性的地图.常见专题地图类型有唯一值渲染,分类渲染,柱状图,饼状图,点密度图 ...

  5. ArcGIS API for JS4.7加载FeatureLayer,点击弹出信息并高亮显示

    我加载的是ArcGIS Server本地发布的FeatureService,ArcGIS API for JS4.7记载FeatureLayer时,在二维需要通过代码启用WebGL渲染,在三维模式下, ...

  6. arcgis中加载google在线地图

    打开arcmap——文件——arcgis online ——搜索china maps 选择china

  7. ArcGIS API For Silverlight使用在线地图的多种方法总结

    引自:http://www.cnblogs.com/meimao5211/p/3283969.html ArcGIS API For Silverlight使用在线地图的多种方法总结 本人也正在学习A ...

  8. ArcGIS API for Silverlight学习笔记

    ArcGIS API for Silverlight学习笔记(一):为什么要用Silverlight API(转) 你用上3G手机了吗?你可能会说,我就是喜欢用nokia1100,ABCDEFG跟我都 ...

  9. Silverlight客户端加载DWG图纸方案

    前段时间一直再研究怎么才能在Silverlight客户端加载 DWG图纸,ArcGIS API For Silverlight中可以加载Shp文件,但是不能加载DWG,最后想出了一个方法步骤如下: 1 ...

随机推荐

  1. 如何在 .Net Framework 4.0 项目上使用 OData?

    最新的 Microsoft ASP.NET Web API 2.1 OData 5.1.0 已只能在 .Net Framework 4.5 的安装了,如果要在 VS2010的 .Net Framewo ...

  2. python 代码片段14

    #coding=utf-8 #enumerate是一个内置函数 data=(123,'abc',3.14) for i,value in enumerate(data): print i,value

  3. TYVJ P1090 母舰 Label:模拟,题目看清就好

    背景 广东汕头聿怀初中 Train#3 Problem 1 描述 在小A的星际大战游戏中,一艘强力的母舰往往决定了一场战争的胜负.一艘母舰的攻击力是普通的MA(Mobile Armor)无法比较的.对 ...

  4. MyEclipse10整合Axis2插件

    1.下载axis2的eclipse插件 2.把下载好的两个插件包解压后放置myeclipse10安装目录下的dropins文件夹中 3.重启MyEclipse10后 File->New-> ...

  5. javascript事件大全4

    javascript事件列表解说 事件 浏览器支持 解说 一般事件 onclick IE3.N2 鼠标点击时触发此事件 ondblclick IE4.N4 鼠标双击时触发此事件 onmousedown ...

  6. Qt 程序退出时断言错误——_BLOCK_TYPE_IS_VALID(pHead->nBlockUse),由setAttribute(Qt::WA_DeleteOnClose)引起

    最近在学习QT,自己仿写了一个简单的QT绘图程序,但是在退出时总是报错,断言错误: 报错主要问题在_BLOCK_TYPE_IS_VALID(pHead->nBlockUse),是在关闭窗口时报的 ...

  7. lucene 3.0.2 搜索

    1.lucene 词频 转载:http://mxdxm.iteye.com/blog/989031 lucene in action作为action系列,确实坚持了其实用性的特色.全书花了很大的篇幅来 ...

  8. windows 通过ssh连接到Linux主机

    1. 确定Linux主机已经开启了ssh功能. 1.1--确认sshserver是否启动 ps -e |grep ssh 如果只有ssh-agent那ssh-server还没有启动,需要/etc/in ...

  9. 【iM_TFTRGB液晶模块】demo例程(版本1.02)发布

    ============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...

  10. Key-Vlaue Coding Apple官方翻译

    今天是键值编码,网上有很多文章,可以百度.不太理解的就看官方文档吧 键-值编码 键值编码是一种运用字符串标识符来间接访问一个对象的属性和关系的机制.它尤其强化并关联了多种Cocoa编程的机制和技术,体 ...