关闭

ArcGIS Api For Flex 动态画点和线(2)

标签: ArcGISapiesri
1934人阅读 评论(0) 收藏 举报
分类:

通过webservicesflex交互取得数据库中的gps坐标动态显示在地图上。

 

 

1.建立数据库:

 

 

2.建立webservices:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.OleDb;
using System.Data;

 

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//
若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。

// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    public Service () {

 

       //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }

 

    [WebMethod]
    public string HelloWorld() {
        return “Hello World”;
    }
    [WebMethod]
    public string GetGPS()
    {
        DataSet myDS;
        string strtemp = string.Empty;
        string myConStr =@”Provider=Microsoft.ACE.OLEDB.12.0;DataSource=D:databaseforgpsDatabase1.accdb;”;
        OleDbConnection myCon = newOleDbConnection(myConStr);
        try   //
异常处理语句

        {
           myCon.Open();
            stringcomStr = “Select * from gps”;
           OleDbDataAdapter myAD = new OleDbDataAdapter(comStr, myCon);
             myDS =new DataSet();
           myAD.Fill(myDS, “gps”);
        }
        catch (OleDbException oe)
        {
            throw oe;
        }
        finally
        {
            //
如果Connection是打开的,则关闭
            if(myCon.State == ConnectionState.Open)
               myCon.Close();
        }
        for (int i = 0; i <myDS.Tables[0].Rows.Count; i++)
        {
            strtemp +=myDS.Tables[0].Rows[i]["X"].ToString() + “,” +myDS.Tables[0].Rows[i]["Y"].ToString()+”/”;
        }
        return strtemp;
    }
   
}
3.
编写flex代码:

 

<?xml version=”1.0″ encoding=”utf-8″?>
<s:Application xmlns:fx=”
http://ns.adobe.com/mxml/2009

      xmlns:s=”library://ns.adobe.com/flex/spark”
      xmlns:mx=”library://ns.adobe.com/flex/mx”xmlns:supportClasses=”com.esri.ags.skins.supportClasses.*” minWidth=”955″minHeight=”600″ xmlns:esri=”
http://www.esri.com/2008/ags

      initialize=”init()”
      >    
 <fx:Script>
  <![CDATA[
   import com.esri.ags.Graphic;
   import com.esri.ags.SpatialReference;
   importcom.esri.ags.components.supportClasses.InfoSymbolWindow;
   import com.esri.ags.geometry.MapPoint;
   import com.esri.ags.geometry.Polyline;
   import com.esri.ags.layers.GraphicsLayer;
   import com.esri.ags.symbols.InfoSymbol;
   import com.esri.ags.symbols.SimpleLineSymbol;
   import com.esri.ags.symbols.SimpleMarkerSymbol;
   import com.esri.ags.symbols.Symbol;
   
   import mx.controls.Alert;
   import mx.rpc.events.FaultEvent;
   import mx.rpc.events.ResultEvent;
   import mx.rpc.soap.WebService;
   private var arr:Array=new Array();
   private var arr2:Array = new Array();
   private var polyine:Polyline=new Polyline();
   private var graphicsLayer:GraphicsLayer=new GraphicsLayer();
   private var graphicsLayertxt:GraphicsLayer=newGraphicsLayer();
   private var grahpic:Graphic=new Graphic();
   private var grahpictxt:Graphic=new Graphic();
   private var strws:String=new String();
   
   private function init():void
   {
    graphicsLayer.add(grahpic);
    Map.addLayer(graphicsLayer);
    graphicsLayertxt.add(grahpictxt);
    Map.addLayer(graphicsLayertxt);
   }     
   private function PlayPoint(e:MouseEvent):void
   {
       var ws:WebService = new WebService();
    ws.loadWSDL("
http://localhost/gps/Service.asmx?wsdl
");
    ws.addEventListener(FaultEvent.FAULT,onFAULT);
    ws.addEventListener(ResultEvent.RESULT,onRESULT);
    ws.GetGPS();
    
   }
   private function PlayLine(e:MouseEvent):void
   {
    
    polyine=new Polyline();
    var i:int=0;
    var timer:Timer = new Timer(1000, arr.length-1);
    timer.addEventListener(TimerEvent.TIMER, TimerMethod);
    timer.start();
    function TimerMethod(event:TimerEvent):void
    {
     var mps:Array=new Array;
     var mpStat:MapPoint=arr[i] as MapPoint;
     var mpEnd:MapPoint=arr[i+1] as MapPoint;
     mps.push(mpStat);
     mps.push(mpEnd);
     polyine.addPath(mps);
       
     grahpic=new Graphic(polyine,sls);
     
     graphicsLayer.add(grahpic);
     i=i+1;
    }
   }
   internal function onFAULT(evt:FaultEvent):void
   {
      Alert.show(evt.fault.faultDetail.toString());
   }
   internal function onRESULT(evt:ResultEvent):void
   {
      //Alert.show(evt.result.toString());
    strws=evt.result.toString();
    arr2=strws.split(“/”);
    for(var j:int=0;j<arr2.length-1;j++)
       {
     varx:String=arr2[j].toString().substr(0,arr2[j].toString().indexOf(“,”));
     vary:String=arr2[j].toString().substr(arr2[j].toString().indexOf(“,”)+1,arr2[j].toString().length-x.length);
     var X:Number = Number(x);
     var Y:Number = Number(y);
     var pt:MapPoint = new MapPoint(X,Y,newSpatialReference(5326));
     arr.push(pt);
     //Alert.show(Y.toString());
    
    }
    
    graphicsLayer.clear();
    var i:int=0;
    var timer:Timer = new Timer(1000, arr.length-1);
    var tmp:MapPoint=arr[i] as MapPoint;
    Map.zoom(-10,tmp);
    timer.addEventListener(TimerEvent.TIMER, TimerMethod);
    timer.start();
    function TimerMethod(event:TimerEvent):void
    {
     var mps:Array=new Array;
     var mpStat:MapPoint=arr[i] as MapPoint;
     
     
     grahpic=new Graphic(mpStat,sps);
     var dt:Date = new Date();
     
     txts.text=”xxx”+”
于:
”+dt.fullYear.toString()+”-”+(dt.month+1).toString()+”-”+dt.date.toString()+”“+dt.hours.toString()+”:”+dt.minutes.toString()+”:”+dt.seconds.toString();
     grahpictxt = new Graphic(mpStat,txts);
     graphicsLayertxt.add(grahpictxt);
     graphicsLayer.add(grahpic);
     Map.centerAt(mpStat);
     i=i+1;
    }
     
   }
     

 

  ]]>
 </fx:Script>
 <fx:Declarations>
  
  <!–
将非可视元素(例如服务、值对象)放在此处
–>
  <!–<s:WebService id=”ws” wsdl=”
http://localhost/gps/Service.asmx?wsdl
“  useProxy=”false”>
   <s:operation name=”GetGPS” fault=”">
  
   </s:operation>

 

  </s:WebService>–>

 

  <esri:SimpleLineSymbolid=”sls”
          width=”3″
          color=”0x00FF00″/>
  <esri:SimpleMarkerSymbol id=”sms”
         color=”0x00FF00″
         size=”12″
         style=”square”/>
  <esri:PictureMarkerSymbol id=”sps” source=”
http://localhost/Red_glow.swf
“  width=”20″ height=”20″ />
  <esri:TextSymbol id=”txts” backgroundColor=”0x00FF00″ color=”0xFF0000″/>

 

 
 </fx:Declarations>
 <esri:Map id=”Map” >
  <esri:ArcGISTiledMapServiceLayer
   url=”
http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaCities_Community_BaseMap_ENG/ShenYang_Community_BaseMap_ENG/MapServer”/
>
  <!–<esri:ArcGISDynamicMapServiceLayer url=”
http://218.60.144.97:8080/RemoteRest/services/LN_SL/MapServer”/>–>

 

 </esri:Map>
 
 
 <mx:ControlBar  width=”100%” top=”0″ left=”0″ paddingTop=”0″paddingBottom=”0″   horizontalAlign=”left” >
  <mx:Button label=”
开始点轨迹
”  width=”100″ fontSize=”12″click=”PlayPoint(event)”/>
  <mx:Button label=”
开始线轨迹
”  width=”100″ fontSize=”12″click=”PlayLine(event)”/>
 </mx:ControlBar>

 

 
</s:Application>

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:415305次
    • 积分:6978
    • 等级:
    • 排名:第3489名
    • 原创:292篇
    • 转载:3篇
    • 译文:1篇
    • 评论:36条
    最新评论