山东大学项目实训12

栅格涂色

空气质量配置参数:
· threshold:代表空气质量指数
· color:代表空气质量对应的颜色
· gradient:空气质量对应的颜色区间
由于颜色不能直接显示在百度地图上,需要将16进制颜色代码转RGB颜色代码

var Color = {
    //16进制颜色代码转RGB颜色代码
    hexToRgb: function (hex) {
        var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
        return result ? {
            r: parseInt(result[1], 16),
            g: parseInt(result[2], 16),
            b: parseInt(result[3], 16)
        } : hex;
    }
    ,
    //RGB颜色代码
    rgb: function (r, g, b) {
        r = Math.floor(r);
        g = Math.floor(g);
        b = Math.floor(b);
        return ["rgb(", r, ",", g, ",", b, ")"].join("");
    },
    //获取区间颜色
    getColor: function (thresholds, value) {
        var filters = $.grep(thresholds, function (item) { return item.threshold >= value })
        return filters.length == 0 ? $(thresholds).last()[0].color : filters[0].color;
    },
    //获取渐变颜色
    getGradientColor: function (thresholds, value) {

        if (!thresholds || thresholds.length == 0) { return; }

        var beginColor = thresholds[0];
        var endColor = null;

        for (var i = thresholds.length - 1; i >= 0; i--) {
            if (thresholds[i].gradient <= value) {
                beginColor = thresholds[i];
                endColor = thresholds[i == 0 ? 0 : i - 1];
                break;
            }
        }

        var diffR = 1;
        var diffG = 1;
        var diffB = 1;

        var perRange = 1.0 / thresholds.length;

        var rate = 1 - (value - beginColor.gradient) / perRange;

        var end = Color.hexToRgb(endColor.color);
        var begin = Color.hexToRgb(beginColor.color);

        //三原色改变步长
        diffR = (end.r - begin.r) * rate;
        diffG = (end.g - begin.g) * rate;
        diffB = (end.b - begin.b) * rate;

        var r = begin.r + Math.ceil(diffR);
        var g = begin.g + Math.ceil(diffG);
        var b = begin.b + Math.ceil(diffB);

        return Color.rgb(r, g, b);
    },
};

同时,在每次绘制图片之前,都需要调用clear()清除原来的图层,避免图层叠加

clear: function () {
        this.tiles = [];
        var $this = this;
        $.each(Object.keys(this.tileLayers), function (i, item) {
            if ($this.tileLayers[item] != null) {
                $this.map.removeOverlay($this.tileLayers[item]);
                $this.tileLayers[item] = null;
            }
        });
        if (this.imageLayer != null) {
            this.map.removeOverlay(this.imageLayer);
            this.imageLayer = null;
        }
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值