[转]展示车辆实时轨迹


/********************************************************/
// 功能:展示车辆实时轨迹
/*******************************************************/

var trackRealtimeIconUrl = "./App_Themes/Vehicles/carRun";//默认的车辆图标
var trackLastIconUrl;
var trackRealtimeRecordNum = 0;
var trackPoint;                 //地图上车辆显示的位置
var trackMap;                   //单独获取的地图
var trackPointGroup;            //地图上点的集合
var lastResult;                 //上次接收到的数据
var trackLine;                  //轨迹线

//获取地图对象
trackMap = $find("TrackRealtimeMap");
if(trackMap != null)
{
    spatialReference = trackMap.get_spatialReference();
    trackMap.add_zoomCompleted(function(){trackMap.refreshGraphics(true)});
    trackMap.add_panCompleted(function(){trackMap.refreshGraphics(true)});
}

//获取地图坐标系统
var spatialReference;
if(trackMap != null)
    spatialReference = trackMap.get_spatialReference();

//此处采用的extjs,也就是等页面内容完全加载后就执行此处代码
Ext.onReady(function() {
    //处理页面传递来的数据
    processTrackRealtimeInfo(passinfo); 
    //并在5秒中后启动实时数据获取
    setTimeout(getTrackRealtimeInfo,5000);   
});


//实时数据获取函数
function getTrackRealtimeInfo()
{
        //此处采用exejs的 Ajax方案,各位可以改用asp.net的Ajax方案
    Coolite.AjaxMethods.GetGpsInfo(tracktmlid,tracklasttime,{success:processTrackRealtimeInfo});//ajax方法请求台数据
    setTimeout(getTrackRealtimeInfo,10000);     //循环调用,时间间隔为10秒钟
}

//处理实时获取的数据
function processTrackRealtimeInfo(result)
{
    if(result.updated == 1)         //获取的数据在更新了的情况下才进行下一步的处理
    {  
        tracklasttime = result.gpstime;          //设置下次获取时的时间                                                      
        if(lastResult != null)                   //要判断当前的数据坐标与上次数据坐标是否相同   
        {   
            if(result.speed == 0 && lastResult.speed == 0)
            {
                //不做地图更新,不做任何事情
            }
            else
            {
                if(lastResult.lon != result.lon || lastResult.lat != result.lat)
                {                                                               
                    updateGPSPosition(result); //在地图上进行绘制
                }
            }                                        
        }
        else
        {                                                                          
            updateGPSPosition(result); //在地图上进行绘制                   
        }      
        lastResult = result;
    }
}

//更新GPS数据在地图上的展示
function updateGPSPosition(result)
{
    //首先判断是否有,
    if(trackMap == null)
    {
        trackMap = $find("TrackRealtimeMap");
        if(trackMap == null)
            return;
        spatialReference = trackMap.get_spatialReference();
        trackMap.add_zoomCompleted(function(){trackMap.refreshGraphics(true)});
        trackMap.add_panCompleted(function(){trackMap.refreshGraphics(true)});
    }
   
    if(trackPoint == null)      //在gps点不存在的情况下,创建它。否则只对其进行更新
    {
        //添加       
         initTrackPointGroup();
         createTrackPoint(result);         
    }
    else
    {       
        updateTrackPoint(result);//更新位置和属性信息
    } 
    updateTrackLine(result);    //更新轨迹线
    //当GPS的当前位置不在视野范围内时,则将其移动到地图中央
    var trackMapEnvelope = trackMap.get_extent();      
    if(result.lon < trackMapEnvelope.get_xmin() ||
        result.lon > trackMapEnvelope.get_xmax() ||
        result.lat < trackMapEnvelope.get_ymin() ||
        result.lat > trackMapEnvelope.get_ymax())
    {
        var pt = new  ESRI.ADF.Geometries.Point(result.lon,result.lat,spatialReference);
        trackMap.panTo(pt,true);             
    }    
    
  
   //强制重新绘制地图上绘制的GraphicFeature
   trackMap.refreshGraphics(true);
}

//在地图上创建点,并设置地图的视野范围
function createTrackPoint(Gpsinfo)
{      
    trackPointGroup.clear();
    trackMap.refreshGraphics(true); 
    var carUrl = getCarIconDirect(trackRealtimeIconUrl ,Gpsinfo.direction);
    trackLastIconUrl = carUrl; 
    var carPoint = new ESRI.ADF.Geometries.Point(Gpsinfo.lon,Gpsinfo.lat,spatialReference);         
    var carMarkerSymbol = new ESRI.ADF.Graphics.MarkerSymbol(carUrl,15,15,"pointer");       //创建非选中状态时的符合显示                     
        
    trackPoint= $create(ESRI.ADF.Graphics.GraphicFeature,{"geometry":carPoint,//创建GraphicFeature,然后将其添加到GraphicsFeatureGroup  
                         "symbol":carMarkerSymbol});


    trackPointGroup.add(trackPoint); 
    trackMap.zoom(trackMap.get_minZoom(),carPoint,false);       
    trackMap.zoom(150,carPoint,false);//250trackMap.get_maxZoom()
    bInit = true;
}

//更新点的信息
function updateTrackPoint(Gpsinfo)
{
    trackMap.refreshGraphics(true);
   
    //获取每个对象的值                         
    var carUrl = getCarIconDirect(trackRealtimeIconUrl,Gpsinfo.direction);   
    //更新坐标信息 
    var pt = new ESRI.ADF.Geometries.Point(Gpsinfo.lon,Gpsinfo.lat,spatialReference);         
    trackPoint.set_geometry(pt);
    if(carUrl != trackLastIconUrl)
    {
        trackPoint.get_symbol().set_imageUrl(carUrl); //更新图片显示
        trackLastIconUrl = carUrl;
    }                    
    trackMap.refreshGraphics(true);         
}

//更新轨迹线
function updateTrackLine(gpsinfo)
{
    if(lastResult == null)  //上次无数据,也就代表着仅仅上传了一次数据,只有一个点无法创建线
        return;
       
    var newpath = new ESRI.ADF.Geometries.CoordinateCollection();
    var newpt =new ESRI.ADF.Geometries.Point(gpsinfo.lon,gpsinfo.lat,spatialReference);
    var lastpt = new ESRI.ADF.Geometries.Point(lastResult.lon,lastResult.lat,spatialReference);
    newpath.add(lastpt);
    newpath.add(newpt);
    if(trackLine != null)   //轨迹存在的情况下则只更新其坐标
    {
        trackLine.get_geometry().addPath(newpath);      
    }
    else
    {   //不存在的情况下创建新的线
        var newPolyline = new ESRI.ADF.Geometries.Polyline(newpath,spatialReference);                     
        var trackLineSymbol = new ESRI.ADF.Graphics.LineSymbol();                     
        trackLineSymbol.set_lineColor('blue');
        trackLineSymbol.set_width(3);
        trackLineSymbol.set_cursor('crosshair');
        trackLineSymbol.set_opacity(0.7);         
        trackLine = $create(ESRI.ADF.Graphics.GraphicFeature,{"id":trackRealtimeRecordNum.toString(),
                                            "geometry":newPolyline,
                                            "symbol":trackLineSymbol});
       
        trackMap.addGraphic(trackLine);
    }      
   
    trackMap.refreshGraphics(true);       
}

//初始化GraphicFeatureGroup
function initTrackPointGroup()
{
    if(trackPointGroup == null)
    {         
        // Use ASP.NET AJAX $create to create a new graphic feature group and define type, properties, events.
        trackPointGroup = $create(ESRI.ADF.Graphics.GraphicFeatureGroup,{"id":"trackPointGroup"});//, "mapTips":mapTips
       
        // Add the graphic feature group to the map.
        trackMap.addGraphic(trackPointGroup);
    }
}

posted @ 2010-05-24 09:43  AooYu  阅读(1573)  评论(0编辑  收藏  举报