addFeature和removeFeature

addFeature和removeFeature

添加小图标到地图上,并移除上一次添加到地图上的小图标

// 点击事件
rowClick(row){
    // 1. 设置地图中心点和缩放级别
    let center = [row.longitude, row.latitude];
    let zoom = 18;
    let view = this.map.getView();
    view.setCenter(center);
    view.setZoom(zoom);

    // 2. 显示当前选择的某一行显示在地图上
    this.addSelectedToMap(row);

    // 3. 清除上一次选择某一行之后出现在地图上的小图标
    this.clearOldIconFeature();
},

addSelectedToMap(row){
    let createLabelStyle1 = function (feature) {
        return new Style({
            image: new Icon(
                ({
                    anchorOrigin: 'top-right',  //图标起点
                    anchorXUnits: 'fraction',   //指定x值为图标点的x值
                    anchorYUnits: 'pixels',     //指定Y值为像素的值
                    offsetOrigin: 'top-right',  //偏移
                    opacity: 1,              //透明度
                    src: '/img/demo.png'      //图标的url
                })),
        });
    };
    let wgsLngLat = [row["longitude"], row["latitude"]];
    let iconFeature = new Feature({
        geometry: new Point(wgsLngLat)
    });
    iconFeature.setStyle(createLabelStyle1(iconFeature));
    this.vectorSource.addFeature(iconFeature);    // 添加iconFeature到地图上
    this.iconFeature = iconFeature;        // 保留添加到map上的iconFeature,等待删除
    this.map.addOverlay(this.overlay);
},

clearOldIconFeature(){
    if(this.iconFeature !== null){
        // listens to the 'addfeature' event once and then removes the preIconFeature.
        this.vectorSource.once('addfeature', (event) => {
            const feature = event.feature;
            this.vectorSource.removeFeature(this.iconFeature);    // 删除上一个iconFeature
        });
    }
},
OpenLayer 文章被收录于专栏

OpenLayer

全部评论

相关推荐

06-25 21:00
门头沟学院 Java
多拆解背记一下当前的高频场景面试题,结合自己的项目经历去作答,面试通过率原来真的不会低!
牛客965593684号:小公司不就是这样的吗,面试要么是点击就送,要么就是往死里拷打,没有一个统一的标准。这个不能代表所有公司
点赞 评论 收藏
分享
不要停下啊:大二打开牛客,你有机会开卷了,卷起来,去找课程学习,在牛客上看看大家面试笔试都需要会什么,岗位有什么需求就去学什么,努力的人就一定会有收获,这句话从来都经得起考验,像我现在大三了啥也不会,被迫强行考研,炼狱难度开局,啥也不会,找工作没希望了,考研有丝丝机会
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务