ArcGIS API for Flex实现的实时监控功能




在项目中涉及到一个实时监控功能,需求是:在一个地图服务中,有一个测站图层,这些测站是一些水位或者雨量测站。测站会定时的获取河流的水情信息,然后发送到数据中心, 所以在系统中也需要定时的去获取每个测站的最新信息,然后直观的体现在地图上面。

思路是:   1.首先在地图加载完成后,获取测站图层里所有测站的编号和位置保存到数组中。

      2.测站编号获取完成后,通过WebService从数据库中获取所以测站的实时信息,信息保存在ArrayCollection中,并在之后定时去获取。

      3.根据测站编号在两个数组中进行匹配,然后用InfoSymbol显示相关信息

先看看效果图~

image

下面是完整的代码

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"    layout="absolute" xmlns:esri="http://www.esri.com/2008/ags" xmlns:ns1="uc.*">
    <mx:Script>
        <![CDATA[
            import uc.MyLabel;
            import com.esri.ags.symbol.InfoSymbol;
            import com.esri.ags.events.LayerEvent;
            import com.esri.ags.symbol.TextSymbol;
            import com.esri.ags.SpatialReference;
            import mx.collections.ArrayCollection;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import com.esri.ags.geometry.MapPoint;
            import com.esri.ags.geometry.Geometry;
            import mx.controls.Alert;
            import com.esri.ags.Graphic;
            import mx.rpc.AsyncResponder;
            import com.esri.ags.tasks.FeatureSet;
            [Bindable]
            public     var information:String=new String();
            var resultlist:Array=new Array();
            var stationInforArr:ArrayCollection=new ArrayCollection();
            private var timer:Timer;
            //定时十分钟获取一次
            private var interval:uint = 600000;

        private function initApp():void
        {
            timer = new Timer(interval);
            timer.addEventListener(TimerEvent.TIMER, getData);
            timer.start();
        }
        function getData(event:TimerEvent):void
        {
            RealInforService.GetStationRealInformation.send();
        }

          //地图加载完成后获取测站信息
            function onComplete(event:LayerEvent):void
            {
                queryTask.url="http://sps-010/ArcGIS/rest/services/cezhan/MapServer/2";
                query.where="STNM <>''";               //查询出所有测站
                //查询结果是否返回Geometry
                 query.returnGeometry=true;
                query.spatialRelationship="esriSpatialRelEnvelopeIntersects";
                //设置要查询的字段
                var fields:Array=new Array();
                fields.push("FID");
                  fields.push("STCD");
                fields.push("STNM");
                fields.push("pinyin");
                //fields.push("POP2000");
                query.outFields=fields;
                //进行查询成功调用onResult方法,错误失败调用onFault
                queryTask.execute(query,new AsyncResponder(onResult,onFault));
            }
            private function onResult(featureSet:FeatureSet,token:Object = null):void
            {
                var geo:Geometry;
                for each(var griphic:Graphic in featureSet.features)
                {
                    myGraphicsLayer.add(griphic);
                    geo= griphic.geometry;
                   switch(geo.type)
                   {
                       case Geometry.MAPPOINT:
                                //转成MapPoint
                            var pnt :MapPoint = geo as MapPoint ;
                                resultlist.push({"编号":griphic.attributes.STCD,"名称":griphic.attributes.STNM,"位置":pnt});
                   }
                }

              //激活计时器
                initApp();

                 //获取实时信息
                  RealInforService.GetStationRealInformation.send();
            }
            //查询失败提示
            private function onFault(info:Object, token:Object = null ):void
            {
                Alert.show("输入的查询语句不正确!");
            }

//根据测站编号进行匹配
            function onSuccess(event:ResultEvent):void
            {
                stationInforArr=event.result.Tables.ds.Rows;
                if(stationInforArr.length>0&&resultlist.length>0)
                {
                    var i:int;
                    var j:int;
                    for(i=0;i<resultlist.length;i++)
                    {
                        for(j=0;j<stationInforArr.length;j++)
                        {
                            if(resultlist[i].编号==stationInforArr[j].STCD)
                            {
                                if(stationInforArr[j].STTP=="PP")
                                {
                                    information="雨量测站("+stationInforArr[j].STNM+")\n"+"日降雨量:"+stationInforArr[j].DRP+"\n实时水位:"+stationInforArr[j].Z+"\n警戒水位:"+stationInforArr[j].WRZ;
                                }else if(stationInforArr[j].STTP=="ZZ")
                                {
                                    information="水位测站("+stationInforArr[j].STNM+")\n"+"日降雨量:"+stationInforArr[j].DRP+"\n实时水位:"+stationInforArr[j].Z+"\n警戒水位:"+stationInforArr[j].WRZ;
                                }
                            var infoSymbol:InfoSymbol = new InfoSymbol();    
                            var infoSymbolFactory:ClassFactory = new ClassFactory(MyLabel);
                            infoSymbolFactory.properties={htmlText:information};
                            infoSymbol.infoRenderer=infoSymbolFactory;
                            //new TextSymbol(information, null, 0x0000FF)
                            var myGraphic:Graphic = new Graphic(new MapPoint(resultlist[i].位置.x,resultlist[i].位置.y), infoSymbol);
                            myGraphicsLayer.add(myGraphic);
                            }
                        }
                    }
                }
            }
            function onStationFault(event:FaultEvent):void
            {
                Alert.show("出错了!");
            }
            function initInformation(event:Event):void
            {
            }
        ]]>
    </mx:Script>
    <mx:WebService id="RealInforService" wsdl="../WebService.asmx?WSDL">
    <mx:operation name="GetStationRealInformation" result="onSuccess(event)" fault="onStationFault(event)">
    </mx:operation>
    </mx:WebService>
    <esri:QueryTask id="queryTask">
    </esri:QueryTask>
    <esri:Query id="query"/>

    <esri:Map x="0" y="0">
        <esri:ArcGISDynamicMapServiceLayer load="onComplete(event)"   url="http://sps-010/ArcGIS/rest/services/cezhan/MapServer"/>
        <esri:GraphicsLayer id="myGraphicsLayer">

        </esri:GraphicsLayer>
    </esri:Map>

</mx:Application>




注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号