//所有车辆的轨迹拖尾类
var TailPolyObjectCollection = function(){
this.tailPolyObjectArray = new Array();
//添加轨迹拖尾对象
this.add = function(obj){
this.tailPolyObjectArray.push(obj);
}
//查找轨迹拖尾对象
this.find = function(code){
for (var i=0;i<this.tailPolyObjectArray.length;i++){
if (this.tailPolyObjectArray[i].code==code){
return this.tailPolyObjectArray[i];
}
}
return null;
}
//重画
this.reDraw = function(){
for (var i=0;i<this.tailPolyObjectArray.length;i++){
this.tailPolyObjectArray[i].reDraw();
}
}
//移除轨迹拖尾对象
this.clear = function(){
for (var i=0;i<this.tailPolyObjectArray.length;i++){
this.tailPolyObjectArray[i].clear();
}
this.tailPolyObjectArray = [];
}
//移除轨迹拖尾轨迹
this.clearPoly = function(){
for (var i=0;i<this.tailPolyObjectArray.length;i++){
this.tailPolyObjectArray[i].clearPoly();
}
}
}
//轨迹拖尾类
var TailPolyObject = function(){
this.code;//车牌号
this.maxPointCount = 30;//最大点个数
this.pointArray = new Array();//点数组
this.pointFixArray = new Array();//点数组(纠偏过的经纬度点)
this.polyLineArray = new Array();//线段数组
this.arrowMarkerArray = new Array();//箭头数组(用于显示拖尾的方向)
this.arrowPointGap = 8;//每8个点一个方箭头
this.flightPath = null;
//添加点
this.addPoint = function(pointFix,point){
//检查新加入的点是否与上一个点相同,如相同则不处理
if (this.pointArray.length > 0){
var lastPoint = this.pointArray[this.pointArray.length-1];
if ((lastPoint.lng() == point.lng()) && (lastPoint.lat() == point.lat())){
return;
}
}
//加入新定位点处理
if (this.pointArray.length == this.maxPointCount){//移除第一个点,后续的点前移,加入新点
delete this.pointArray[0];
delete this.pointFixArray[0];
for (var i=0;i<this.pointArray.length-1;i++){
this.pointArray[i]=this.pointArray[i+1];
this.pointFixArray[i]=this.pointFixArray[i+1];
}
this.pointArray[this.maxPointCount-1]=point;
this.pointFixArray[this.maxPointCount-1]=pointFix;
}else{//加入新点
this.pointArray.push(point);
this.pointFixArray.push(pointFix);
}
var pArr = null;
if (currentMapTypeId == google.maps.MapTypeId.ROADMAP){//街景(使用纠正后的坐标)
pArr = this.pointFixArray;
}else if (currentMapTypeId == google.maps.MapTypeId.HYBRID){//卫星影像(使用未纠正的坐标)
pArr = this.pointArray;
}
if (pArr.length>1){
if (this.pointArray.length==2){//开始创建拖尾
this.flightPath = new google.maps.Polyline({
path: pArr,
strokeColor: "red",
strokeOpacity: 1.0,
strokeWeight: 3
});
this.flightPath.setMap(map);
}else{//更新拖尾
this.reDraw();
}
}
//if (pArr.length > 3){
// this.drawArrow(pArr);
//}
}
//改变地图类型
this.reDraw = function(){
var pArr = null;
if (currentMapTypeId == google.maps.MapTypeId.ROADMAP){//街景(使用纠正后的坐标)
pArr = this.pointFixArray;
}else if (currentMapTypeId == google.maps.MapTypeId.HYBRID){//卫星影像(使用未纠正的坐标)
pArr = this.pointArray;
}
if (this.flightPath!=null){
var path = this.flightPath.getPath();
path = [];
for (var i=0;i<pArr.length;i++){
path.push(pArr[i]);
}
this.flightPath.setPath(path);
}
}
//画方向箭头
this.drawArrow = function(pArr){
if (pArr.length < 3) return;
//处理箭头
for (var i=0;i<this.arrowMarkerArray.length;i++){
this.arrowMarkerArray[i].setMap(null);
}
this.arrowMarkerArray = [];
//重新画箭头
for (var i=1;i<pArr.length-1;i=i+this.arrowPointGap){
var p1=pArr[i-1];
var p2=pArr[i+1];
var dir = bearing(p1,p2);
var num = parseInt(dir+"");
if (dir>0 && dir<90){
num = 45;
}else if (dir>90 && dir<180){
num = 135;
}else if (dir>180 && dir<270){
num = 225;
}else if (dir>270 && dir<360){
num = 315;
}
var icon = rootPath + "/images/dir/dir_"+num+".png";
var geomarker = new google.maps.Marker({
position: pArr[i],
map: map,
icon: icon
});
this.arrowMarkerArray.push(geomarker);
}
}
//清除
this.clear = function(){
this.pointArray = [];
this.pointFixArray = [];
this.clearPoly();
}
//清除轨迹拖尾
this.clearPoly = function(){
this.polyLineArray = [];
if (this.flightPath!=null){
this.flightPath.setMap(null);
}
//处理箭头
for (var i=0;i<this.arrowMarkerArray.length;i++){
this.arrowMarkerArray[i].setMap(null);
}
this.arrowMarkerArray = [];
}
}
var tailPolyObjects = new TailPolyObjectCollection();