Esri为我们提供了TiledMapServiceLayer类,这个类正是所有瓦片地图服务都必须继承的类,像ArcGISTiledMapServiceLayer, OpenStreetMapLayer, VETiledLayer等esri提供的瓦片类都是继承了该类的,所以我们要扩展该TiledMapServiceLayer类
一.调用天地图服务(SXGeoTileLayer类)
import com.esri.ags.SpatialReference; import com.esri.ags.geometry.Extent; import com.esri.ags.geometry.MapPoint; import com.esri.ags.layers.supportClasses.LOD; import com.esri.ags.layers.supportClasses.TileInfo; import com.esri.ags.layers.TiledMapServiceLayer; import com.esri.ags.layers.TiledMapServiceLayer; import flash.geom.Point; import flash.net.URLRequest; /** * SXGeoTileLayer */ public class SXGeoTileLayer extends TiledMapServiceLayer { //-------------------------------------------------------------------------- // // Constructor // //-------------------------------------------------------------------------- /** * Creates a new PortlandTiledMapServiceLayer object. */ public function SXGeoTileLayer() { super(); buildTileInfo(); // to create our hardcoded tileInfo setLoaded(true); // Map will only use loaded layers } //-------------------------------------------------------------------------- // // Variables // //-------------------------------------------------------------------------- private var wkid:int = 4326; private var _tileInfo:TileInfo = new TileInfo(); // see buildTileInfo() private var _url:String = new String(); //private var _wmsLayer:String = new String(); //-------------------------------------------------------------------------- // // Overridden properties // fullExtent() // initialExtent() // spatialReference() // tileInfo() // units() // //-------------------------------------------------------------------------- //---------------------------------- // fullExtent // - required to calculate the tiles to use //---------------------------------- override public function get fullExtent():Extent { return new Extent(-180, -90, 180, 90, new SpatialReference(4326)); //return new Extent(65.94,18.84,137.74,62.85, new SpatialReference(4326)); } //---------------------------------- // initialExtent // - needed if Map doesn't have an extent //---------------------------------- override public function get initialExtent():Extent { return new Extent(-124.731422,24.955967,-66.969849, 49.371735,new SpatialReference(4326)); //return new Extent(112.48,47.77,112.60,47.97, new SpatialReference(4326)); } //---------------------------------- // spatialReference // - needed if Map doesn't have a spatialReference //---------------------------------- override public function get spatialReference():SpatialReference { return new SpatialReference(4326); } //---------------------------------- // tileInfo //---------------------------------- override public function get tileInfo():TileInfo { return _tileInfo; } //---------------------------------- // units // - needed if Map doesn't have it set //---------------------------------- override public function get units():String { return "esriDecimalDegrees"; } //-------------------------------------------------------------------------- // // Overridden methods // getTileURL(level:Number, row:Number, col:Number):URLRequest // //-------------------------------------------------------------------------- override protected function getTileURL(level:Number, row:Number, col:Number):URLRequest { var url:String ; url = _url + "&X=" + col.toString() + "&Y=" + row.toString()+ "&L="+ level.toString(); return new URLRequest(url) ; } //-------------------------------------------------------------------------- // // Private Methods // //-------------------------------------------------------------------------- private function buildTileInfo():void { _tileInfo.height = 256; _tileInfo.width = 256; _tileInfo.origin = new MapPoint(-180, 90); _tileInfo.spatialReference = new SpatialReference(4326); _tileInfo.lods = [ //new LOD(0, 1.40625, 590995197.1416691), new LOD(1, 0.703125, 295497598.5708346), new LOD(2, 0.3515625, 147748799.2854173), new LOD(3, 0.17578125, 73874399.64270864), new LOD(4, 0.087890625, 36937199.82135432), new LOD(5, 0.0439453125, 18468599.91067716), new LOD(6, 0.02197265625, 9234299.95533858), new LOD(7, 0.010986328125, 4617149.97766929), new LOD(8, 0.0054931640625, 2308574.98883465), new LOD(9, 0.00274658203124999, 1154287.49441732), new LOD(10, 0.001373291015625, 577143.747208662), new LOD(11, 0.0006866455078125, 288571.873604331), new LOD(12, 0.000343322753906249, 144285.936802165), new LOD(13, 0.000171661376953125, 72142.9684010827), new LOD(14, 0.0000858306884765626, 36071.4842005414), new LOD(15, 0.0000429153442382813, 18035.7421002707), new LOD(16, 0.0000214576721191406, 9017.87105013534), ]; } private function padString(text:String, size:int, ch:String):String { while (text.length < size) { text = ch + text; } return text; } public function set url(val:String):void { this._url = val; } }使用方法:
var wmslayer:SXGeoTileLayer=new SXGeoTileLayer(); wmslayer.url=" http://tile0.tianditu.com/DataServer?T=A0610_ImgAnno"; map.addLayer(wmslayer);
import com.esri.ags.SpatialReference; import com.esri.ags.geometry.Extent; import com.esri.ags.geometry.MapPoint; import com.esri.ags.layers.TiledMapServiceLayer; import com.esri.ags.layers.supportClasses.*; import flash.net.URLRequest; //扩展TiledMapServiceLayer图层实现加载google地图 public class GoogleMapLayer extends TiledMapServiceLayer { private var _tileInfo:TileInfo=new TileInfo(); private var _baseURL:String=""; public function GoogleMapLayer() { super(); buildTileInfo(); setLoaded(true); } override public function get fullExtent():Extent { return new Extent(-20037508.342787, -20037508.342787, 20037508.342787, 20037508.342787, new SpatialReference(102113)); } override public function get initialExtent():Extent { return new Extent(-20037508.342787, -20037508.342787, 20037508.342787, 20037508.342787, new SpatialReference(102113)); } override public function get spatialReference():SpatialReference { return new SpatialReference(102113); } override public function get tileInfo():com.esri.ags.layers.supportClasses.TileInfo { return _tileInfo; } //获取矢量地图 override protected function getTileURL(level:Number, row:Number, col:Number):URLRequest { var url:String=_baseURL&+ "x=" + col + "&" + "y=" + row + "&" + "z=" + level+ "&s="; return new URLRequest(url); } public function set url(val:String):void { this._baseURL = val; } private function buildTileInfo():void { _tileInfo.height=256; _tileInfo.width=256; _tileInfo.origin=new MapPoint(-20037508.342787, 20037508.342787); _tileInfo.spatialReference=new SpatialReference(102113); _tileInfo.lods = [ new LOD(0, 156543.033928, 591657527.591555), new LOD(1, 78271.5169639999, 295828763.795777), new LOD(2, 39135.7584820001, 147914381.897889), new LOD(3, 19567.8792409999, 73957190.948944), new LOD(4, 9783.93962049996, 36978595.474472), new LOD(5, 4891.96981024998, 18489297.737236), new LOD(6, 2445.98490512499, 9244648.868618), new LOD(7, 1222.99245256249, 4622324.434309), new LOD(8, 611.49622628138, 2311162.217155), new LOD(9, 305.748113140558, 1155581.108577), new LOD(10, 152.874056570411, 577790.554289), new LOD(11, 76.4370282850732, 288895.277144), new LOD(12, 38.2185141425366, 144447.638572), new LOD(13, 19.1092570712683, 72223.819286), new LOD(14, 9.55462853563415, 36111.909643), new LOD(15, 4.77731426794937, 18055.954822), new LOD(16, 2.38865713397468, 9027.977411), new LOD(17, 1.19432856685505, 4513.988705), new LOD(18, 0.597164283559817, 2256.994353), new LOD(19, 0.298582141647617, 1128.497176) ]; } }
使用方法:
var wmslayer:GoogleMapLayer =new GoogleMapLayer(); wmslayer.url="http://khm2.google.com/kh/v=38&hl=zh-CN"; map.addLayer(wmslayer);