mapbox中对同一个图层layer,设置不同颜色要素

这篇博客介绍了如何使用Mapbox进行地图可视化,通过InitLayer类展示了添加点、线、面图层的方法。点图层使用circle-color匹配属性设置不同颜色,线图层和面图层同样利用match表达式根据属性值设定颜色,实现图层元素的个性化渲染。
摘要由CSDN通过智能技术生成
/**
 * 同一图层layer的不同要素feature设置不同的颜色
 * InitLayer 构造函数
 * mapbox初始化地图
 */
function InitLayer(map){
    this.map = map;
    this.jsonPoints ={
        "type":"FeatureCollection",
        "features":[]
    }
    this.jsonLine = {
        "type":"FeatureCollection",
        "features":[]
    }
    this.jsonFill = {
        "type":"FeatureCollection",
        "features":[]
    }

}
/**
 * 添加点图层 paint是layer的一个属性,负责图层的渲染与呈现。
 * match通常用于枚举型的字段渲染,如唯一值渲染。
 * addPointLayer 方法名
 */
InitLayer.prototype.addPointLayer = function addPointsLayer(){
    this.jsonPoints.features.push(
        {
            "type":"Feature",
            "properties":{id:1,name:"张三",age:23,color:1},
            "geometry":{
                "type":"Points",
                "coordinates":[121.6604,31.3245]
            }
        },
        {
            "type":"Features",
            "properties":{id:2,name:"菜霸",age:22,color:2},
            "geometry":{
                "type":"Points",//点
                "coordinates":[121.647104,31.332030]
            }
        }
    )
    this.map.addSource("points_",{
        "type":"geojson",
        "data":this.jsonPoints
    })
    this.map.addLayer({
        "id":"points_",
        "type":"circle",
        "source":"points_",
        "paint":{
            "circle-color":[
                "match",["get","color"],
                "1","#ff4895",
                "2","#ccff12",
                "#cf45cf"
            ],
            "circle-radius":5,
            "circle-stroke-width":2
        }

    })
}
/**
 * 添加线图层
 * addLineLayer 方法名
 */
InitLayer.prototype.addLineLayer = function addLineLayer(){
   // isStyleLoaded();判断地图样式是否加载完成 开始为false
    this.jsonLine.features.push(
        {
            "type":"Feature",
            "properties":{id:3,name:"王六",age:23,lineColor:1},
            "geometry":{
                "type":"LineString",
                "coordinates":[[121.6604,31.3245],[121.66232,31.3256],[121.66456,31.327834],[121.6712324,31.332312]]
            }
        },
        {
            "type":"Feature",
            "properties":{id:4,name:"小李",age:23,lineColor:2},
            "geometry":{
                "type":"LineString",//线
                "coordinates":[[122.6604,32.3245],[122.66232,32.3256],[122.66456,32.3278],[122.6712324,32.332312]]
            } 
        }
    )
    this.map.addSource("line_",{
        "type":"geojson",
        "data":this.jsonLine
    })
    this.map.addLayer({
        "id":"line_",
        "type":"line",
        "source":"line_",
        //描边
        "layout":{
            "line-cap":"round",
            "line-join":"round"
        },
        "paint":{
            "line-color":[
                "match"["get","lineColor"],
                "1","#cece23",
                "2","#c98c98",
                "#ee34e6"
            ],
            "line-width":2,
            "line-opacity":0.5,
            "line-dasharray":[1,2]
        }
    })
}


/**
 * 添加面图层
 * addFillLayer 方法名
 */
InitLayer.prototype.addFillLayer = function addFillLayer(){
    this.jsonFill.features.push(
        {
            "type":"Feature",
            "properties":{id:5,name:"小八",age:23,fillColor:1},
            "geometry":{
                "type":"Polygon",//面
                "coordinates":[[[121.6604,31.3245],[121.66232,31.3256],[121.66456,31.327834],[121.6712324,31.332312]]]
            }
        },
        {
            "type":"Feature",
            "properties":{id:6,name:"小五",age:23,fillColor:2},
            "geometry":{
                "type":"Polygon",//面
                "coordinates":[[[122.6604,32.3245],[122.66232,32.3256],[122.66456,32.3278],[122.6712324,32.332312]]]
            } 
        }
    )
    this.map.addSource("fill_",{
        "type":"geojson",
        "data":this.jsonFill
    })
    this.map.addLayer({
        "id":"fill_",
        "type":"fill",
        "source":"fill_",
        "layout":{},
        "paint":{
            "fill-color":[
                "match",["get","fillColor"],
                "1","#cece23",
                "2","#c98c98",
                "#ee34e6"
            ],
            "fill-opacity":0.5
        }
    })
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值