ArcGIS API For Flex 扩展:DynamicLayer扩展-wmsLayer实现

 非常感谢ESRI社区上看到的这篇文章,比Arcgis官网的例子要好懂。如下:

在ArcGIS API for Flex中,经常会遇到需要扩展DynamicMapServiceLayer的情况,下面就介绍一下
如何扩展DynamicMapServiceLayer,以调用WMS服务为例:
1 定义WMSMapServiceLayer类,并继承DynamicMapServiceLayer
2 定义一些需要的参数作为属性,当然也可以不定义
3 重载loadMapImage函数,在该函数中返回你需要的图片。
详细代码如下:

  
  
  
  
  1. package 
  2.     import flash.display.Loader; 
  3.     import flash.events.Event; 
  4.     import flash.net.URLRequest; 
  5.  import com.esri.ags.layers.DynamicMapServiceLayer; 
  6.  
  7.  public class WMSMapServiceLayer extends DynamicMapServiceLayer 
  8.  { 
  9.    
  10.   internal const service:String = "WMS"
  11.         internal const version:String = "1.1.1"
  12.         internal const request:String = "GetMap"
  13.         internal const scalar:Number = 1; 
  14.          
  15.         public var format:String = "image/jpeg"
  16.         public var wmsLayer:String; 
  17.         public var serviceName:String; 
  18.          
  19.         public var wmtVersion:String; 
  20.         public var styles:String = ""
  21.         public var srs:String = "EPSG:4326"
  22.         public var url:String; 
  23.         public var transparentBG:String; 
  24.          
  25.          
  26.   public function WMSMapServiceLayer() 
  27.   { 
  28.    super(); 
  29.    this.setLoaded(true); 
  30.    this.url = url; 
  31.    this.wmsLayer = wmsLayer; 
  32.   } 
  33.   override protected function loadMapImage(loader:Loader):void { 
  34.             // Get our pixel dimensions. 
  35.             var pxWidth:Number = Math.floor(this.map.width * scalar); 
  36.             var pxHeight:Number = Math.floor(this.map.height * scalar); 
  37.             // Build the GetMap request 
  38.             var index : int = url.indexOf( "?"); 
  39.             var prefix : String = index == -1 ? "?" : "&";            
  40.             var _url:String = url; 
  41.             _url += prefix + "SERVICE="+service; 
  42.             _url += "&VERSION="+version; 
  43.             _url += "&REQUEST="+request; 
  44.             // For many WMS servers, SERVICENAME and others are unused, but some must be included or the server will reject our request. For example STYLES. 
  45.             if( serviceName != null) { 
  46.                 _url += "&SERVICENAME="+serviceName; 
  47.             } 
  48.             if( wmtVersion != null) { 
  49.                  _url += "&WMTVER="+wmtVersion; 
  50.             } 
  51.             if( wmsLayer != null) { 
  52.                  _url += "&LAYERS="+wmsLayer; 
  53.             } 
  54.             _url += "&STYLES="+styles; 
  55.             if(transparentBG != null) { 
  56.                 _url += "&TRANSPARENT="+transparentBG; 
  57.             } 
  58.             if( srs != null) { 
  59.                 _url += "&SRS="+srs; 
  60.             } 
  61.             _url += "&FORMAT="+format; 
  62.             _url += "&WIDTH="+pxWidth; 
  63.             _url += "&HEIGHT="+pxHeight; 
  64.             _url += "&BBOX="+this.map.extent.xmin+","+this.map.extent.ymin+","+this.map.extent.xmax+","+this.map.extent.ymax; 
  65.             // We create a new URLRequest 
  66.             var wmsReq:URLRequest = new URLRequest(_url); 
  67.             // And finally pass it to our parent as a Loader. This URL will be then loaded into the map window. 
  68.             loader.load(wmsReq); 
  69.   } 
  70.    
  71.  } 

出处:http://bbs.esrichina-bj.cn/esri/viewthread.php?tid=60673

之后根据项目的要求,自己依样画葫芦弄了一个,终于把师兄提供的WMS服务给调出来了。不过现在只是简单实现了调用,还不会更多的操作,有待进一步学习。代码如下:

 

  
  
  
  
  1. package com.surname.business 
  2.     import com.esri.ags.SpatialReference; 
  3.     import com.esri.ags.Units; 
  4.     import com.esri.ags.geometry.Extent; 
  5.     import com.esri.ags.layers.DynamicMapServiceLayer; 
  6.      
  7.     import flash.display.Loader; 
  8.     import flash.net.URLRequest; 
  9.     import flash.net.URLVariables; 
  10.      
  11.     public class WMSMapServiceLayer extends DynamicMapServiceLayer 
  12.     { 
  13.         internal const request:String = "GetMap"
  14.         internal const scalar:Number = 1; 
  15.          
  16.         public var format:String = "image/png"
  17.         public var layers:String="vge:ming1"
  18.         public var wmsLayer:String; 
  19.         public var serviceName:String; 
  20.          
  21.         public var wmtVersion:String; 
  22.         public var styles:String = ""
  23.         public var srs:String = "EPSG:4326"
  24.         public var url:String="http://132.1.10.83:8080/geoserver/wms?"
  25.         public var transparentBG:String; 
  26.          
  27.         public function WMSMapServiceLayer() 
  28.         { 
  29.             super(); 
  30.             this.setLoaded(true); 
  31.             this.url=url; 
  32.             this.wmsLayer=wmsLayer; 
  33.         } 
  34.          
  35.         override protected function loadMapImage(loader:Loader):void 
  36.         { 
  37.             // Get our pixel dimensions. 
  38.             var pxWidth:Number = Math.floor(this.map.width * scalar); 
  39.             var pxHeight:Number = Math.floor(this.map.height * scalar); 
  40.             // Build the GetMap request 
  41.             var _url:String = url; 
  42.             _url += "&BBOX="+"65.22020225524902,16.255382919311515,148.47830696105956,58.26974830627439"
  43.             _url += "&STYLES="+styles; 
  44.             _url += "&FORMAT="+format; 
  45.             _url += "&REQUEST="+request; 
  46.             if( wmsLayer != null) { 
  47.                 _url += "&LAYERS="+wmsLayer; 
  48.             } 
  49.             _url += "&WIDTH="+pxWidth; 
  50.             _url += "&HEIGHT="+pxHeight; 
  51.             if( srs != null) { 
  52.                 _url += "&SRS="+srs; 
  53.             } 
  54.  
  55.             // We create a new URLRequest 
  56.             var wmsReq:URLRequest = new URLRequest(_url); 
  57.             // And finally pass it to our parent as a Loader. This URL will be then loaded into the map window. 
  58.             loader.load(wmsReq); 
  59.         } 
  60.          
  61.     } 

 

你可能感兴趣的:(api,Flex,职场,for,arcgis,休闲,wmsLayer)