﻿//如果地图移动，则最佳值为step=10; steptime=100;
function adjustBusStep() {
    if (runBus && runBus.b_running) {
        runBus.freshBusStep();
    }
}
function RunBus(b_pointAry){
    var b_step = 10; //用于控制平滑,值越小,跑起来越平滑
    var b_steptime = 100;  //控制速度,值越小,小车跑的越快

    var b_imgSize = [80, 25]; //小车图片的大小
    var b_points = new Array(); //总线路坐标序列
    var b_stepx = 1; 
    var b_stepy = 1; //小车每次行走的距离x,y偏移量
    var b_currentX = 20;
    var b_currentY = 20; //当前小车位置
    var b_i = 0; //数组索引
    var b_next = true; //是否开始下一条线路

    var b_interval;
    var b_runline;

    var b_busmarker;
    var b_runtype = 0; //轨迹模拟类型；0小车移动；1地图移动
    var b_running = false;
    var lfttop;

    function drawRunLine() {

        for (var i = 0; i < b_pointAry.length; i++) {
            var latlng = new GLatLng(b_pointAry[i][0], b_pointAry[i][1]);
            b_points.push(latlng);
        }

        if (!b_points || b_points.length < 2)
            return false;

        if (b_runline)
            map.removeOverlay(b_runline);

        b_runline = new GPolyline(b_points, "#0071FD", 5, 0.8, { clickable: false, geodesic: false });
        map.addOverlay(b_runline);

        return true;
    }

    this.runbus = function() {
    if (!b_pointAry || b_pointAry.length == 0) return;
        
        b_running = true;
        
        if (b_runtype == 0) {
            b_step = 10;
            b_steptime = 100;
        }
        else {
            b_step = 10;
            b_steptime = 100;
            addBusDiv();
        }

        var bsuc = drawRunLine();

        if (!bsuc) return;

        b_interval = window.setInterval("runBus.running()", b_steptime);
    }
    
    function addBusDiv() {
        if (!$("busImg")) {
            lfttop = getOffset($("map"));
            var busimg = document.createElement("img");
            busimg.src = "images/bus/Bus0.png";
            busimg.id = "busImg";
            busimg.style.position = "absolute";
            busimg.style.filter = "progid:DXImageTransform.Microsoft.Chroma(color='#C2CBCF')";
            document.body.appendChild(busimg);
        }
    }
    this.freshBusStep = function() {
    if (!b_pointAry || b_pointAry.length == 0) return;

        var point1 = b_points[b_i];
        var point2 = b_points[b_i + 1];

        var clientp1 = map.fromLatLngToContainerPixel(point1);
        var clientp2 = map.fromLatLngToContainerPixel(point2);

        var clientlength = Math.sqrt(Math.pow((clientp2.x - clientp1.x), 2) + Math.pow((clientp2.y - clientp1.y), 2));

        b_stepx = (point2.lng() - point1.lng()) * b_step / clientlength;
        b_stepy = (point2.lat() - point1.lat()) * b_step / clientlength;

    }
    //调整小车图片
    function AdjustBusImg (p1, p2) {

        var x1, x2, y1, y2;

        x1 = p1.x;
        y1 = p1.y;
        x2 = p2.x;
        y2 = p2.y;

        var lineangle = Math.atan((y2 - y1) / (x2 - x1));

        if ((x2 - x1) < 0 && (y2 - y1) < 0) {
            lineangle = Math.PI - lineangle;
        }
        else if ((x2 - x1) < 0) {
            lineangle = Math.PI - lineangle;
        }
        else if ((y2 - y1) < 0) {
            lineangle = -lineangle;
        }
        else {
            lineangle = 2 * Math.PI - lineangle;
        }

        var angleNum = getDu(lineangle * 180 / Math.PI);

        //得到当前图片的宽高，用js自动获取的方法会出现错误
        getImgWH(angleNum);

        return angleNum;

    }

    function getDu(du) {
        var tempdu = du;

        if (du % 15 != 0) {
            var maxint = Math.floor(du / 15);
            if ((maxint + 1) * 15 - du < du - maxint * 15) {
                tempdu = (maxint + 1) * 15;
            }
            else {
                tempdu = maxint * 15;
            }
        }

        if (tempdu == 360)
            tempdu = 0;

        return tempdu;
    }
    this.stop = function() {
        if (b_interval)
            window.clearInterval(b_interval);
        if (b_busmarker)
            map.removeOverlay(b_busmarker);
        if (b_runline)
            map.removeOverlay(b_runline);
        b_points = [];
        b_running = false;
    }

    this.running = function() {

        //continue from first point
        if (b_i >= b_points.length - 1) {
            b_i = 0;

            if (b_runtype == 1)
                map.setCenter(b_points[0]);

            b_next = true;
        }

        var point1 = b_points[b_i];
        var point2 = b_points[b_i + 1];

        //开始新的线段
        if (b_next) {

            b_currentX = point1.lng();
            b_currentY = point1.lat();

            var clientp1 = map.fromLatLngToContainerPixel(point1);
            var clientp2 = map.fromLatLngToContainerPixel(point2);

            var clientlength = Math.sqrt(Math.pow((clientp2.x - clientp1.x), 2) + Math.pow((clientp2.y - clientp1.y), 2));
            if (clientlength < 1) {
                b_next = true; //开始新的线段
                b_i++;
                return;
            } else {
                b_stepx = (point2.lng() - point1.lng()) * b_step / clientlength;
                b_stepy = (point2.lat() - point1.lat()) * b_step / clientlength;

                var angleNum = AdjustBusImg(clientp1, clientp2); //调整小车角度,并计算偏移量
                if (b_runtype == 0) {
                    if (b_busmarker)
                        map.removeOverlay(b_busmarker);

                    var icon = new GIcon(G_DEFAULT_ICON);
                    icon.shadow = "";
                    icon.iconAnchor = new GPoint(b_imgSize[0] / 2, 2 * b_imgSize[1] / 3);
                    icon.image = "images/bus/Bus" + angleNum + ".png";
                    icon.iconSize = new GSize(b_imgSize[0], b_imgSize[1]);

                    b_busmarker = new GMarker(point1, icon);
                    map.addOverlay(b_busmarker);
                }
                else {
                    $("busImg").style.width = b_imgSize[0] + "px";
                    $("busImg").style.height = b_imgSize[1] + "px";
                    $("busImg").style.left = (lfttop[1] + map.getSize().width / 2 - b_imgSize[0] / 2) + "px";
                    $("busImg").style.top = (lfttop[0] + map.getSize().height / 2 - b_imgSize[1] / 2) + "px";
                    $("busImg").src = "images/bus/Bus" + angleNum + ".png";
                }

                b_next = false;
            }
        }

        var gp1 = map.fromLatLngToDivPixel(point1);
        var gp2 = map.fromLatLngToDivPixel(point2);
        if (Math.abs(gp1.x - gp2.x) < 2 && Math.abs(gp1.y - gp2.y) < 2) {
            if (b_runtype == 0) {
                b_busmarker.setPoint(new GLatLng(point2.lat(), point2.lng()));
            }
            else {
                map.panTo(new GLatLng(point2.lat(), point2.lng()));
            }
            b_next = true; //开始新的线段
            b_i++;
        } else {
            //判断线段是否结束
            var p1x = point1.lng();
            var p1y = point1.lat();
            if (Math.abs(b_currentX - p1x) >= Math.abs(point2.lng() - p1x) && Math.abs(b_currentY - p1y) >= Math.abs(point2.lat() - p1y)) {
                if (b_runtype == 0) {
                    b_busmarker.setPoint(new GLatLng(point2.lat(), point2.lng()));
                }
                else {
                    map.panTo(new GLatLng(point2.lat(), point2.lng()));
                }
                b_next = true; //开始新的线段
                b_i++;
            }
            else {
                if (b_runtype == 0) {
                    b_busmarker.setPoint(new GLatLng(b_currentY, b_currentX));
                }
                else {
                    map.panTo(new GLatLng(b_currentY, b_currentX));
                }
                b_currentX += b_stepx;
                b_currentY += b_stepy;
            }
        }
    }

    function getImgWH(anglenum) {
        var imgwidth,imgheight;
        switch (anglenum) {
            case 0:
            case 180:
                imgwidth = 80;
                imgheight = 25;
                break;
            case 90:
            case 270:
                imgwidth = 25;
                imgheight = 80;
                break;
            case 75:
            case 255:
            case 105:
            case 285:
                imgwidth = 45;
                imgheight = 84;
                break;
            case 60:
            case 120:
            case 240:
            case 300:
                imgwidth = 62;
                imgheight = 82;
                break;
            case 45:
            case 135:
            case 225:
            case 315:
                imgwidth = 75;
                imgheight = 75;
                break;
            case 30:
            case 150:
            case 210:
            case 330:
                imgwidth = 82;
                imgheight = 62;
                break;
            case 15:
            case 165:
            case 195:
            case 345:
                imgwidth = 84;
                imgheight = 45;
                break;
            default:
                break;
        }
        b_imgSize[0] = imgwidth;
        b_imgSize[1] = imgheight;
    }
}
