---
title: "初探DeepEarth控件"
author: "holoblog"
date: "2009-12-25T01:22:00Z"
tags: []
slug: "deepearth"
draft: false
lastmod: "2011-11-20T20:33:50Z"
id: 1269014
url: "https://holoblog.iteye.com/blog/1269014"
---

DeepEarth是一个地图控件，它将微软的[Virtual Earth](http://www.microsoft.com/VirtualEarth/)与Silverlight 2.0联合起来。该开源[项目](http://www.codeplex.com/deepearth "项目")由其创建者（一群.NET的狂热爱好者）在CodePlex上发布。这句话已经在一年前就已经在网上随处可见了，去年12月InfoQ的张龙老师就翻译了一篇关于DeepEarth的文章资料[《DeepEarth：使用Silverlight的地图控件》，](http://www.infoq.com/cn/news/2008/12/DeepEarth)当时我也又关注不过一直都没有花时间去研究它，最近写Bing Maps开发文章很多朋友都问我有研究过DeepEarth的话题，为了帮助这些朋友解决一些困扰特把DeepEarth简单学习了下特此分享给大家，关于DeepEarth的最新信息可访问：[http://deepearth.codeplex.com/](http://deepearth.codeplex.com/)进行了解。

　　DeepEarth的最新版本是1.1，功能上可以说是很强大和完善了，提供了对多种地图数据服务的支持，包括Google Maps,YaHoo,Bing Maps,MapInfo以及amazonaws等等。本文通过DeepEarth加载amazonaws的卫星地图的示例程序初步探索下DeepEarth的基本使用方法。

　　首先建立Silverlight项目以及Siverlight宿主Web应用程序，附加DeepEarth源代码项目到解决方案中，并添加项目引用到新建的Siverlight应用，项目解决方案如下。

　　　　　　　　![](http://images.cnblogs.com/cnblogs_com/beniao/BingMaps/DeepEarth1.jpg)

　　

　　到这里一个基本的入门工作已经准备好了，接下来就是如何使用DeepEarth，首先得在新建的Silverlight的MainPage.xaml里声明DeepEarth的引用如下：

&lt;!--&lt;br/ /&gt;&lt;br/ /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br/ /&gt;http://www.CodeHighlighter.com/&lt;br/ /&gt;&lt;br/ /&gt;--&gt;1xmlns:DeepEarth="clr-namespace:DeepEarth;assembly=DeepEarth"  
2xmlns:DeepBlueMarble="clr-namespace:DeepEarth.Provider.BlueMarble;assembly=DeepEarth.Provider"  
3xmlns:DeepControls="clr-namespace:DeepEarth.Controls;assembly=DeepEarth"

　　有了引用的声明下面就可以使用DeepEarth控件以及其他的一些辅助控件（功能导航控件），具体使用如下代码块：

&lt;!--&lt;br/ /&gt;&lt;br/ /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br/ /&gt;http://www.CodeHighlighter.com/&lt;br/ /&gt;&lt;br/ /&gt;--&gt;1&lt;Gridx:Name="LayoutRoot"Width="550"Height="500"&gt;  
2&lt;DeepEarth:Mapx:Name="map"&gt;  
3&lt;DeepControls:NavControlCanvas.ZIndex="1003"&gt;  
4&lt;/DeepControls:NavControl&gt;  
5&lt;DeepControls:CoordControl/&gt;  
6&lt;/DeepEarth:Map&gt;  
7&lt;/Grid&gt;

　　前端开发就这样了，现在转到后台代码视图并编写如下代码：

&lt;!--&lt;br/ /&gt;&lt;br/ /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br/ /&gt;http://www.CodeHighlighter.com/&lt;br/ /&gt;&lt;br/ /&gt;--&gt;1publicpartialclassMainPage:UserControl  
2{  
3publicMainPage()  
4{  
5InitializeComponent();  
6if(HtmlPage.IsEnabled)  
7{  
8map.BaseLayer.Source=newBmTileSource(BmMapModes.BlueMarbleWeb);  
9map.Events.MapLoaded+=PageLoad;  
10}  
11}  
12  
13protectedvoidPageLoad(Mapm,MapEventArgsargs)  
14{  
15//定位地图的经度和纬度坐标  
16doublelongitude=double.Parse("106.5883");  
17doublelatitude=double.Parse("29.5345");  
18m.SetViewCenter(newPoint(longitude,latitude),8);  
19}  
20}

　　或许有人会说，代码我都能看懂，这到底代表什么意思呢？其实很简单这段后台代码就是给前端的DeepEarth控件初始化了一个地图图层，并为地图控件的加载事件委托了处理函数，当地图控件运行初始化时将地图定位到指定的经度和纬度，以及地图所显示的放大级别。

　　主要的实现还是在BmTileSource这个地图图层源里，也可以叫他地图图片系统源或地图图片映射系统源吧。关于TileSource可查看我的另一篇文章：[【Silverlight】Bing Maps学习系列（七）：使用Bing Maps的图片系统（Tile System）](http://www.cnblogs.com/beniao/archive/2009/12/19/1627938.html)

　　现在我们将目光转移到BmTileSource类里去，位于DeepEarth.Provider.BlueMarble目录下，你将会看到这样一句代码：

&lt;!--&lt;br/ /&gt;&lt;br/ /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br/ /&gt;http://www.CodeHighlighter.com/&lt;br/ /&gt;&lt;br/ /&gt;--&gt;privateconststringTilePathBlueMarbleWeb=@"http://s3.amazonaws.com/com.modestmaps.bluemarble/{0}-r{2}-c{1}.jpg";

　　这就是amazonaws卫星地图数据的Tile System（图片系统）的映射Url，通过这个Url我们就可以使用DeepEarth控件去加载amazonaws的卫星地图，这里需要注意的就是BmTileSource类里的GetTile方法，它就是用于计算地图图片系统的图片映射Url的。

&lt;!--&lt;br/ /&gt;&lt;br/ /&gt;Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br/ /&gt;http://www.CodeHighlighter.com/&lt;br/ /&gt;&lt;br/ /&gt;--&gt;1publicoverrideUriGetTile(inttileLevel,inttilePositionX,inttilePositionY)  
2{  
3if(IsInitialized)  
4{  
5intzoom=TileToZoom(tileLevel);  
6\_IsTileDownloadStarted=true;  
7  
8stringurl=string.Empty;  
9  
10switch(MapMode)  
11{  
12caseBmMapModes.BlueMarbleWeb:  
13url=TilePathBlueMarbleWeb;  
14url=string.Format(url,zoom,tilePositionX,tilePositionY);  
15break;  
16  
17caseBmMapModes.BlueMarbleLocal:  
18url=TilePathBlueMarbleLocal;  
19intport=Application.Current.Host.Source.Port;  
20url=string.Format(url,zoom,tilePositionX,tilePositionY,port);  
21break;  
22}  
23returnnewUri(url);  
24}  
25returnnull;  
26}

　　根据不同的参数，组合为一个又一个完整的地图底片映射地址，然后组合在一起就显示出了完整的地图。编译运行这个示例我们可看到如下效果：

　　　　　　　　![](http://images.cnblogs.com/cnblogs_com/beniao/BingMaps/DeepEarth2.jpg)

　　本篇就介绍到这里，关于DeepEarth的更多开发介绍请.....

　　DeepEarth加载微软Bing Maps在线示例：[http://deepearth.soulsolutions.com.au/](http://deepearth.soulsolutions.com.au/)

　　

　　本文示例代码下载：[DeepEarthDemo.rar](http://files.cnblogs.com/beniao/Maps/DeepEarthDemo.rar)