ArcGIS API for Silverlight开发入门(7):使用非AGS数据源的图层

        通过上一节学习,可以看出在Silverlight API中不仅可以轻松使用ArcGIS Server 9.3发布的地图服务,也可以通过继承相应的图层,引入其他的数据源,比如ArcGIS Server 9.2发布的地图服务,WMS服务,或者其他免费的数据。本节就通过一个实例,来看看如何将Google Map作为底图数据。
        Google Map是经过缓存的数据,所以需要继承的是TiledMapServiceLayer。那么在扩展这个图层的时候需要做哪些工作呢?首先就要明白地图缓存的原理。可以看出我们继承的这个图层,需要收集到以下几个信息:
1、Tiling Scheme Origin;
2、切图的范围,也就是FullExtent;
3、SpatialReference;
4、TileInfo,包括切图的大小,级数,以及每级的Resolution;
5、最后就是重写GetTileUrl方法。
        这是为什么呢?可以想象,当地图控件的范围改变时,能够获取到当前范围的信息,那么只要把左上角和右下角之间的Tile全部按顺序显示出来就行了。由前面的文章可以看出,当图层获取了1、2、3、4四个信息后,图层完全可以自动计算出所需的Tile,最后根据GetTileUrl方法取回这些Tile显示出来即可。
        那么对于Google Map的前4个参数,如何取得呢?记得在Catalog中做缓存时,有一个Load Tiling Scheme from Google Map吗?按照这个Tiling Scheme将一个地图服务做缓存,然后查看它的conf.xml和Service Directory,便完全可以取得这几个参数了。另外关于如何获取Google Map的缓存,网上已经有非常多方法,这里就不再讨论了。


        代码如下:
public class GoogleMap:TiledMapServiceLayer
{
public override void Initialize()
{
this.FullExtent = new ESRI.ArcGIS.Geometry.Envelope(-20037508.342787,-20037508.342787,20037508.342787,20037508.342787);//(-180, -85.0511287798066,180, 85.0511287798066)
{
SpatialReference = new ESRI.ArcGIS.Geometry.SpatialReference(102113);
};
this.SpatialReference = new ESRI.ArcGIS.Geometry.SpatialReference(102113);
//this.InitialExtent = this.FullExtent;
this.TileInfo = new TileInfo()
{
Height = 256,
Width = 256,
Origin = new ESRI.ArcGIS.Geometry.MapPoint(-20037508.342787, 20037508.342787)//Origin = new ESRI.ArcGIS.Geometry.MapPoint(-180, 90)
{
SpatialReference = new ESRI.ArcGIS.Geometry.SpatialReference(102113)
},
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();
}

public override string GetTileUrl(int level, int row, int col)
{
//google maps map
//string baseUrl = "http://mt0.google.com/mt/v=ap.92&hl;=zh-CN&x;=";
//string url = baseUrl + col.ToString() + "&y;=" + row.ToString() + "&z;=" + level.ToString() + "&s;=";
//return url;

////google maps satallite
string baseUrl = "http://khm2.google.com/kh/v=38&hl;=zh-CN&x;=";
string url = baseUrl + col.ToString() + "&y;=" + row.ToString() + "&z;=" + level.ToString() + "&s;=";
return url;
}

}
        需要注意一点,Google Map采用的是WGS 1984 Web Mercator投影,这个投影的wkid在REST API中查不到,但在Service Direcotry中可以找到,是102113。另外,重写DynamicMapServiceLayer也是基本相同的。
        之后也可以按照这个Tiling Scheme对自己的服务作缓存,自己的数据和Google Map便可以叠加在一起了。但是这样子使用Google Map的数据不仅担心会被封IP,而且更重要的是版权问题,毕竟不像JS API(有ArcGIS JavaScript Extension for the Google Maps API )或者Flex API(有Google Map API for Flex)。别忘了MS有自己的Virtual Earth,下一节中就来看看如何在我们的程序中名正言顺的使用VE的数据吧。

3 thoughts on “ArcGIS API for Silverlight开发入门(7):使用非AGS数据源的图层”

Leave a Reply to fatfatrat Cancel Reply

Your email address will not be published. Required fields are marked *