在WebGIS项目中,我们可能经常碰到一些需要在地图上进行动态绘制一些轨迹路线,如GPS轨迹,公交线路路线,流量走向等。而VML是最简单的实现方式,VML的全称是Vector Markup Language(矢量可标记语言),矢量的图形,意味着图形可以任意放大缩小而不损失图形的质量,这在制作地图上有很大用途。
实际应用中我们经常会是这样实施,先获得轨迹的地理坐标,然后通过接口转换成屏幕坐标(如AGS里面的ToScreenPoint) ,将屏幕坐标数组传到js中进行绘制。这里通过js动态添加vml线的步长,实现动态绘制轨迹路线。大家还可以对其进行扩展,比如到某个节点时进行Tips提示等。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
line=document.getElementById("pLine");//VML线
2
line.style.position="absolute";
3
with(line.stroke)
4![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
5
Color="red";
6
Weight="2px";
7
EndArrow="Classic";
8
DashStyle="Solid";
9
}
10
var html="10,10";//起始点的字符串
11
var prePoint=[10,10];//起始点
12
var pointArray=new Array();//轨迹点坐标
13
pointArray[0]=[10,10];
14
pointArray[1]=[155,366];
15
pointArray[2]=[250,450];
16
pointArray[3]=[44,150];
17
var currentIndex=0;//当前轨迹段
18
var step=5;//步长
19
function Move()
20![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
21
var p=prePoint;
22
var firstPoint=pointArray[currentIndex];
23
var nextPoint=pointArray[currentIndex+1];
24
if(!nextPoint)
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
26
clearInterval(donghua);
27
return;
28
}
29
cosdirection=(nextPoint[0]-firstPoint[0])/Math.sqrt(Math.pow((-nextPoint[1]+firstPoint[1]),2)+Math.pow((nextPoint[0]-firstPoint[0]),2));
30![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
sindirection=(-nextPoint[1]+firstPoint[1])/Math.sqrt(Math.pow((-nextPoint[1]+firstPoint[1]),2)+Math.pow((nextPoint[0]-firstPoint[0]),2));
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
var x=step*cosdirection;//步长step的x增量
34
var y=step*sindirection;//步长step的y增量
35
if(Math.abs(p[0]-nextPoint[0])<step || Math.abs(p[1]-nextPoint[1])<step)//如果接近下一个节点,但不足一个步长,直接跳到下一个节点,并开始下一段的绘制。
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
37
currentIndex++;
38
if(Math.abs(p[0]-nextPoint[0])<step)
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
x=Math.abs(p[0]-nextPoint[0])*cosdirection;
41
y=Math.abs(p[0]-nextPoint[0])*sindirection;
42
}
43
else if(Math.abs(p[1]-nextPoint[1])<step)
44![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45
x=Math.abs(p[1]-nextPoint[1])*cosdirection;
46
y=Math.abs(p[1]-nextPoint[1])*sindirection;
47
}
48![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
}
50
p[0]+=x;
51
p[1]-=y;
52
p[0]=Math.round(p[0]);
53
p[1]=Math.round(p[1]);
54
if(p[0]>=0&&p[1]>=0&&step>0)
55![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
56
prePoint=p;
57
html+=" "+prePoint[0]+","+prePoint[1];//绘制vml线
58
line.Points.value=html;
59
}
60
}
61
var donghua=window.setInterval(Move,100);
62
document.body.focus();
63![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)