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

2023-11-19

/**
 * 同一图层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
        }
    })
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

mapbox中对同一个图层layer,设置不同颜色要素 的相关文章

  • struts2-输入校验、xml校验的使用

    1 输入校验 直接在Action类里添加相应的方法 这里定义了一个中间变量 前面我直接返回SUCCESS ERROR会报错 具体我还要研究一下 struts xml login jsp 2 xml校验 在XXAction的同级目录下 建立X
  • Qt Desginer布局方法

    关于Qt Desginer中的布局方法 网上教程少之又少 个人经过反复的实践和摸索 觉得可以用一句话来概括 先不断地进行小布局 然后对整体进行大布局 先不断地进行小布局的目的就是将同为一组的控件按某个格式排列使界面干净有序 同时方便以后对整
  • [教程]Hexo + Github 搭建自己的专属博客

    教程 Hexo Github 搭建自己的专属博客 文章目录 教程 Hexo Github 搭建自己的专属博客 1 安装Git和NodeJS 2 安装Hexo 3 加载主题 4 修改主题配置 5 将博客部署在GitHub上 6 写文章并上传
  • 在Mac上安装MongoDB

    1 访问MongoDB官方下载地址 http www mongodb org downloads 2 解压文件mongodb osx ssl x86 64 4 0 9 tgz 解压之后会变成mongodb osx x86 64 4 0 9文

随机推荐