ArcGIS Flex API 调用天地图和Google地图服务
Esri为我们提供了TiledMapServiceLayer类,这个类正是所有瓦片地图服务都必须继承的类,像ArcGISTiledMapServiceLayer, OpenStreetMapLayer, VETiledLayer等esri提供的瓦片类都是继承了该类的,所以我们要扩展该TiledMapServiceLayer类 一.调用天地图服务(SXGeoTileLayer类) [plain] view plaincopyprint?
1. import com.esri.ags.SpatialReference; 2. import com.esri.ags.geometry.Extent; 3. import com.esri.ags.geometry.MapPoint;
4. import com.esri.ags.layers.supportClasses.LOD; 5. import com.esri.ags.layers.supportClasses.TileInfo; 6. import com.esri.ags.layers.TiledMapServiceLayer; 7. import com.esri.ags.layers.TiledMapServiceLayer; 8.
9. import flash.geom.Point; 10. import flash.net.URLRequest; 11. 12. /**
13. * SXGeoTileLayer 14. */
15. public class SXGeoTileLayer extends TiledMapServiceLayer 16. {
17. //-------------------------------------------------------------------------- 18. //
19. // Constructor 20. //
21. //-------------------------------------------------------------------------- 22. 23. /**
24. * Creates a new PortlandTiledMapServiceLayer object. 25. */
26. public function SXGeoTileLayer() 27. {
28. super(); 29.
30. buildTileInfo(); // to create our hardcoded tileInfo 31.
32. setLoaded(true); // Map will only use loaded layers 33. } 34.
35. //-------------------------------------------------------------------------- 36. //
37. // Variables
38. //
39. //-------------------------------------------------------------------------- 40.
41. private var wkid:int = 4326;
42. private var _tileInfo:TileInfo = new TileInfo(); // see buildTileInfo() 43. private var _url:String = new String();
44. //private var _wmsLayer:String = new String(); 45.
46. //-------------------------------------------------------------------------- 47. //
48. // Overridden properties 49. // fullExtent() 50. // initialExtent() 51. // spatialReference() 52. // tileInfo() 53. // units() 54. //
55. //-------------------------------------------------------------------------- 56. 57.
58. //---------------------------------- 59. // fullExtent
60. // - required to calculate the tiles to use 61. //---------------------------------- 62.
63. override public function get fullExtent():Extent 64. {
65. return new Extent(-180, -90, 180, 90, new SpatialReference(4326));
66. //return new Extent(65.94,18.84,137.74,62.85, new SpatialReference(4326)); 67. } 68.
69. //---------------------------------- 70. // initialExtent
71. // - needed if Map doesn't have an extent 72. //---------------------------------- 73.
74. override public function get initialExtent():Extent 75. {
76. return new Extent(-124.731422,24.955967,-66.969849, 49.371735,new SpatialReferenc
e(4326));
77. //return new Extent(112.48,47.77,112.60,47.97, new SpatialReference(4326)); 78. } 79.
80. //----------------------------------
81. // spatialReference
82. // - needed if Map doesn't have a spatialReference 83. //---------------------------------- 84.
85. override public function get spatialReference():SpatialReference 86. {
87. return new SpatialReference(4326); 88. } 89.
90. //---------------------------------- 91. // tileInfo
92. //---------------------------------- 93.
94. override public function get tileInfo():TileInfo 95. {
96. return _tileInfo; 97. } 98.
99. //---------------------------------- 100. // units
101. // - needed if Map doesn't have it set 102. //---------------------------------- 103.
104. override public function get units():String 105. {
106. return \107. } 108.
109. //-------------------------------------------------------------------------- 110. //
111. // Overridden methods
112. // getTileURL(level:Number, row:Number, col:Number):URLRequest 113. //
114. //-------------------------------------------------------------------------- 115.
116. override protected function getTileURL(level:Number, row:Number, col:Number):URLR
equest 117. {
118. var url:String ;
119. url = _url + \120. return new URLRequest(url) ; 121. } 122.
123. //--------------------------------------------------------------------------
124. //
125. // Private Methods 126. //
127. //-------------------------------------------------------------------------- 128.
129. private function buildTileInfo():void 130. {
131. _tileInfo.height = 256; 132. _tileInfo.width = 256;
133. _tileInfo.origin = new MapPoint(-180, 90);
134. _tileInfo.spatialReference = new SpatialReference(4326); 135. _tileInfo.lods = [
136. //new LOD(0, 1.40625, 590995197.1416691), 137. new LOD(1, 0.703125, 295497598.5708346), 138. new LOD(2, 0.3515625, 147748799.2854173), 139. new LOD(3, 0.17578125, 73874399.64270864), 140. new LOD(4, 0.087890625, 36937199.82135432), 141. new LOD(5, 0.0439453125, 18468599.91067716), 142. new LOD(6, 0.02197265625, 9234299.95533858), 143.
144. new LOD(7, 0.010986328125, 4617149.97766929), 145. new LOD(8, 0.0054931640625, 2308574.98883465), 146. new LOD(9, 0.00274658203124999, 1154287.49441732), 147. new LOD(10, 0.001373291015625, 577143.747208662), 148. new LOD(11, 0.0006866455078125, 288571.873604331), 149. new LOD(12, 0.000343322753906249, 144285.936802165), 150. new LOD(13, 0.000171661376953125, 72142.9684010827), 151. new LOD(14, 0.0000858306884765626, 36071.4842005414), 152. new LOD(15, 0.0000429153442382813, 18035.7421002707), 153. new LOD(16, 0.0000214576721191406, 9017.87105013534), 154. ]; 155. } 156.
157. private function padString(text:String, size:int, ch:String):String 158. {
159. while (text.length < size) 160. {
161. text = ch + text; 162. }
163. return text; 164. }
165. public function set url(val:String):void 166. {
167. this._url = val;
168. } 169. } 使用方法:
[plain] view plaincopyprint?
1. var wmslayer:SXGeoTileLayer=new SXGeoTileLayer();
2. wmslayer.url=\3. map.addLayer(wmslayer);
二.调用Google地图服务 [plain] view plaincopyprint?
1. import com.esri.ags.SpatialReference; 2. import com.esri.ags.geometry.Extent; 3. import com.esri.ags.geometry.MapPoint;
4. import com.esri.ags.layers.TiledMapServiceLayer; 5. import com.esri.ags.layers.supportClasses.*; 6.
7. import flash.net.URLRequest; 8.
9. //扩展TiledMapServiceLayer图层实现加载google地图 10. public class GoogleMapLayer extends TiledMapServiceLayer 11. {
12. private var _tileInfo:TileInfo=new TileInfo(); 13. private var _baseURL:String=\14.
15. public function GoogleMapLayer() 16. {
17. super();
18. buildTileInfo(); 19. setLoaded(true); 20. } 21.
22. override public function get fullExtent():Extent 23. {
24. return new Extent(-20037508.342787, -20037508.342787, 20037508.342787, 2003
7508.342787, new SpatialReference(102113)); 25. } 26.
27. override public function get initialExtent():Extent 28. {
29. return new Extent(-20037508.342787, -20037508.342787, 20037508.342787, 2003
7508.342787, new SpatialReference(102113)); 30. } 31.
32. override public function get spatialReference():SpatialReference
33. {
34. return new SpatialReference(102113); 35. } 36.
37. override public function get tileInfo():com.esri.ags.layers.supportClasses.TileInfo 38. {
39. return _tileInfo; 40. } 41.
42. //获取矢量地图
43. override protected function getTileURL(level:Number, row:Number, col:Number):URL
Request 44. {
45. var url:String=_baseURL&+ 46. \47. \48. \49.
50. return new URLRequest(url); 51. }
52. public function set url(val:String):void 53.
54. this._baseURL = val; 55.
56. private function buildTileInfo():void 57. {
58. _tileInfo.height=256; 59. _tileInfo.width=256;
60. _tileInfo.origin=new MapPoint(-20037508.342787, 20037508.342787); 61. _tileInfo.spatialReference=new SpatialReference(102113); 62. _tileInfo.lods = [
63. new LOD(0, 156543.033928, 591657527.591555), 64. new LOD(1, 78271.5169639999, 295828763.795777), 65. new LOD(2, 39135.7584820001, 147914381.897889), 66. new LOD(3, 19567.8792409999, 73957190.948944), 67. new LOD(4, 9783.93962049996, 36978595.474472), 68. new LOD(5, 4891.96981024998, 18489297.737236), 69. new LOD(6, 2445.98490512499, 9244648.868618), 70. new LOD(7, 1222.99245256249, 4622324.434309), 71. new LOD(8, 611.49622628138, 2311162.217155), 72. new LOD(9, 305.748113140558, 1155581.108577), 73. new LOD(10, 152.874056570411, 577790.554289), 74. new LOD(11, 76.4370282850732, 288895.277144), 75. new LOD(12, 38.2185141425366, 144447.638572),
76. new LOD(13, 19.1092570712683, 72223.819286), 77. new LOD(14, 9.55462853563415, 36111.909643), 78. new LOD(15, 4.77731426794937, 18055.954822), 79. new LOD(16, 2.38865713397468, 9027.977411), 80. new LOD(17, 1.19432856685505, 4513.988705), 81. new LOD(18, 0.597164283559817, 2256.994353), 82. new LOD(19, 0.298582141647617, 1128.497176) 83. ]; 84. } 85. }
使用方法:
[plain] view plaincopyprint?
1. var wmslayer:GoogleMapLayer =new GoogleMapLayer(); 2. wmslayer.url=\3. map.addLayer(wmslayer);