map.js 7.41 KB
var gb_real_gps_map = (function () {

    var wrap = '#real_gps_data_page';
    var map;

    var init = function (lineCode, upDown, stations) {
        //初始化地图
        var point = new BMap.Point(121.544336, 31.221315)
            , zoom = 18;

        map = new BMap.Map($('.map_cont:eq(0)', wrap)[0]);
        map.centerAndZoom(point, zoom);
        map.enableScrollWheelZoom(true);

        if (stations)
            drawLine(lineCode, upDown, stations);
    }

    var drawLine = function (lineCode, upDown, data) {
        map.clearOverlays();
        gps_marker_arr = [];
        var arr = data.stations[upDown];

        drawStations(arr);//绘制站点
        drawSections(arr);//绘制站点间路段

        //定位到起点
        map.setCenter(
            new BMap.Point(arr[0].bd_lon, arr[0].bd_lat));
    };


    function drawStations(arr) {
        var s, point, marker, w, iw, icon, text;
        for (var i = 0, len = arr.length; i < len; i++) {
            s = arr[i];
            s.index = i + 1;
            transCoord(s);

            point = new BMap.Point(s.bd_lon, s.bd_lat);
            marker = new BMap.Marker(point);

            //根据站点名称 计算marker 宽度
            w = s.name.length * 12 + 38;
            iw = w - 2;

            s._name = s.name;
            icon = new BMap.Icon(gb_map_utils.createStationIcon(s, w)
                , new BMap.Size(iw, 24), {anchor: new BMap.Size(iw / 2, 25)})
            marker.setIcon(icon);
            marker._data = s;
            map.addOverlay(marker);
        }
    }

    function drawSections(arr) {
        var s, coords, pos, polyline;
        var color = arr[0].upDown == 0 ? 'blue' : 'red'
            , style = {strokeWeight: 5, strokeColor: color, strokeOpacity: .7};

        for (var i = 0, len = arr.length; i < len; i++) {
            s = arr[i];
            pos = [];
            $.each(s.paths, function () {
                coords = TransGPS.wgsToBD(this.lat, this.lon);
                pos.push(new BMap.Point(coords.lng, coords.lat));
            });

            polyline = new BMap.Polyline(pos, style);
            map.addOverlay(polyline);
        }
    }

    function transCoord(obj) {
        if (obj.bd_lat)
            return;
        var coord = TransGPS.wgsToBD(obj.lat, obj.lon);
        obj.bd_lat = coord.lat;
        obj.bd_lon = coord.lng;
    }

    var gps_marker_arr = {}, topMarker;
    var renderGps = function (arr) {
        var devices = [];
        var gps, m, w;
        for (var i = 0, len = arr.length; i < len; i++) {
            gps = arr[i];
            //转换坐标
            transCoord(gps);

            m = gps_marker_arr[gps.deviceId];

            if (!m) {
                m = createGpsMarker(gps);
                map.addOverlay(m);
                gps_marker_arr[gps.deviceId] = m;
            }
            else if (gps.timestamp != m._data.timestamp) {
                m.setPosition(new BMap.Point(gps.bd_lon, gps.bd_lat));
                w = m._icon_width;
                m.setIcon(new BMap.Icon(createCarIconRotation(gps, w), new BMap.Size(w, 70)));

                m._data = gps;
                if (m.infoWindow.isOpen())
                    bdOpenWindow(m);
            }

            devices.push(gps.deviceId);
        }

        //删除多余的Marker (改变走向的)
        for (var k in gps_marker_arr) {
            if (devices.indexOf(k) == -1) {
                map.removeOverlay(gps_marker_arr[k]);
                gps_marker_arr[k] = null;
                delete gps_marker_arr[k];
            }
        }
    };

    var createGpsMarker = function (gps) {
        var point = new BMap.Point(gps.bd_lon, gps.bd_lat)
            , m = new BMap.Marker(point);

        //根据编码长度 计算marker 宽度
        var w = gps.nbbm.length * 8.85;
        m._icon_width = w;
        //渲染icon
        m.setIcon(new BMap.Icon(createCarIconRotation(gps, w), new BMap.Size(w, 70)));
        m.setTop(true);
        m._data = gps;

        m.infoWindow = new BMap.InfoWindow();
        //click
        m.addEventListener('click', function () {
            bdOpenWindow(this);
        });
        //mouseover
        m.addEventListener('mouseover', function () {
            setTop(this);
        });
        return m;
    };

    function setTop(m) {
        if (topMarker)
            topMarker.setTop(false);
        m.setTop(true);
        topMarker = m;
    }

    function bdOpenWindow(marker) {
        var gps = marker._data;

        marker.infoWindow.setContent(template('gps_map_info_win-temp', gps));
        map.openInfoWindow(marker.infoWindow, marker.point);
    }

    function createCarIconRotation(gps, w) {
        if (!w)
            w = 70;

        var canvas = $('<canvas></canvas>')[0];
        canvas.width = w;
        canvas.height = 64;
        var ctx = canvas.getContext('2d');

        var bg_color = gps.upDown == 0 ? 'rgba(94, 150, 210, 1)' : 'rgba(201, 33, 33, 1)';

        ctx.roundRect(0, 0, w, 19, 4).stroke();
        ctx.fillStyle = bg_color;
        ctx.fill();
        //文字
        ctx.font = "14px arial";
        ctx.fillStyle = "#fff";
        ctx.fillText(gps.nbbm, 2, 14);

        //角度图标
        ctx.save();
        ctx.translate(canvas.width / 2, canvas.height / 2);
        ctx.rotate(gps.direction * Math.PI / 180);
        /**
         * 绘制角度图标
         */
        ctx.beginPath();
        ctx.strokeStyle = bg_color;
        var circle = {
            x: 0,    //圆心的x轴坐标值
            y: 0,    //圆心的y轴坐标值
            r: 12      //圆的半径
        };
        var d = 12.5;
        //绘制圆形
        ctx.arc(circle.x, circle.y, circle.r, 0, Math.PI * 2, true);
        //按照指定的路径绘制弧线
        ctx.stroke();
        ctx.fillStyle = "#ffffff";
        ctx.fill();
        //下方矩形
        ctx.beginPath();
        ctx.fillStyle = bg_color;
        ctx.fillRect(5 - d, 16 - d, 15, 4);
        ctx.rect(5 - d, 11 - d, 7.5, 8);
        ctx.rect(12.5 - d, 11 - d, 7.5, 8);
        ctx.strokeStyle = bg_color;
        ctx.stroke();
        //上方三角形
        ctx.beginPath();
        ctx.fillStyle = "#111111";
        ctx.moveTo(12.5 - d, 3 - d);
        ctx.lineTo(6.5 - d, 7.5 - d);
        ctx.lineTo(18.5 - d, 7.5 - d);
        ctx.closePath();
        ctx.fill();

        ctx.restore();
        return canvas.toDataURL();
    }

    //Canvas 带圆角的矩形
    CanvasRenderingContext2D.prototype.roundRect = function (x, y, w, h, r) {
        if (w < 2 * r) r = w / 2;
        if (h < 2 * r) r = h / 2;
        this.strokeStyle = 'rgba(0,102,0,.1)';
        this.beginPath();
        this.moveTo(x + r, y);
        this.arcTo(x + w, y, x + w, y + h, r);
        this.arcTo(x + w, y + h, x, y + h, r);
        this.arcTo(x, y + h, x, y, r);
        this.arcTo(x, y, x + w, y, r);
        this.closePath();
        return this;
    };


    /**
     * 定位到gps
     * @param device
     */
    var focus = function (device) {
        var m = gps_marker_arr[device];
        if (m) {
            map.setCenter(m.getPosition());
            bdOpenWindow(m);
        }
    }
    return {
        init: init,
        drawLine: drawLine,
        renderGps: renderGps,
        focus: focus
    }
})();