首页技术文章正文

react 之 openlayer图形交互编辑【黑马web前端】

更新时间:2019年07月26日 10时53分39秒 来源:黑马程序员论坛

一、引入依赖的库文件

// 设置地图背景色
import React, { Component } from 'react';
import Map from '../../component/map/map';
// import style from './map.css'
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import { Vector as VectorSource } from 'ol/source';
import { Vector } from 'ol/layer';
import { Style, Fill, Stroke, Circle } from 'ol/style';
import { Draw, Modify , Snap,Select} from 'ol/interaction';
二、渲染地图组件和按钮组件

render() {
    return (
      <div>
        <button onClick={this.addGraph.bind(this, 1)}>绘制点</button>
        <button onClick={this.addGraph.bind(this, 2)}>绘制线</button>
        <button onClick={this.addGraph.bind(this, 3)}>绘制多边形</button>
        <button onClick={this.addGraph.bind(this, 4)}>绘制圆</button>
        {/* <button onClick={this.addGraph.bind(this, 5)}>添加矩形</button>
        <button onClick={this.addGraph.bind(this, 6)}>添加多边形</button> */}
        <Map ref="map" center={{ lon: 113.8, lat: 34.6 }} />;
      </div>
    );
  }
三、地图的加载和绘制图层的创建

componentDidMount() {
    // console.log(bg)
    let { map } = this.refs.map;
    let { mapkey } = window.config;
    var TiandiMap_vec = new TileLayer({
      name: '天地图矢量图层',
      source: new XYZ({
        url: 'http://t0.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=' + mapkey, //mapkey为天地图密钥
        wrapX: false,
      }),
    });
    var TiandiMap_cva = new TileLayer({
      name: '天地图矢量注记图层',
      source: new XYZ({
        url: 'http://t0.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=' + mapkey, //mapkey为天地图密钥
        wrapX: false,
      }),
    });
    // 添加绘制图层
    var source = new VectorSource({ wrapX: false });
    var vector = new Vector({
        source: source,
        style: new Style({
            fill: new Fill({
                color: 'rgba(255, 255, 255, 0.2)'
            }),
            stroke: new Stroke({
                color: '#ffcc33',
                width: 2
            }),
            image: new Circle({
                radius: 7,
                fill: new Fill({
                    color: '#ffcc33'
                })
            })
        })
    });
    //将绘制层添加到地图容器中

    // 加载数据
    map.addLayer(TiandiMap_vec);
    map.addLayer(TiandiMap_cva);
    map.addLayer(vector);

    var modify = new Modify({source: source});
    map.addInteraction(modify);



    this.setState({
      TiandiMap_vec,
      TiandiMap_cva,
      vector,
      source
    });
  }
四、绘制图层和添加鼠标编辑事件

addGraph = type => {
    let { map } = this.refs.map;
    let that = this
    let drawType = "None"
    switch (type) {
      case 1:
            drawType="Point"
        break;
      case 2:
           drawType="LineString"
        break;
      case 3:
            drawType="Polygon"
        break;
      case 4:
            drawType="Circle"
        break;
      case 5:
        break;
      case 6:
        break;
      default:
        break;
    }


    var draw = new Draw({
        //绘制层数据源
        source:that.state.source ,
        /** @type {ol.geom.GeometryType}几何图形类型 */
        type:drawType,
        // //几何信息变更时调用函数
        // geometryFunction: geometryFunction,
        //最大点数
        // maxPoints: 2
        //  设置后移动鼠标绘制图形
        // freehand: true
    });
    map.addInteraction(draw);
    let snap = new Snap({source: that.state.source});
    map.addInteraction(snap);

  };
五、全部代码

// 设置地图背景色
import React, { Component } from 'react';
import Map from '../../component/map/map';
// import style from './map.css'
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import { Vector as VectorSource } from 'ol/source';
import { Vector } from 'ol/layer';
import { Style, Fill, Stroke, Circle } from 'ol/style';
import { Draw, Modify , Snap,Select} from 'ol/interaction';


class DrawEditGraph extends Component {
  constructor(props) {
    super(props);
    this.state = {};
  }
  componentDidMount() {
    // console.log(bg)
    let { map } = this.refs.map;
    let { mapkey } = window.config;
    var TiandiMap_vec = new TileLayer({
      name: '天地图矢量图层',
      source: new XYZ({
        url: 'http://t0.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=' + mapkey, //mapkey为天地图密钥
        wrapX: false,
      }),
    });
    var TiandiMap_cva = new TileLayer({
      name: '天地图矢量注记图层',
      source: new XYZ({
        url: 'http://t0.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=' + mapkey, //mapkey为天地图密钥
        wrapX: false,
      }),
    });
    // 添加绘制图层
    var source = new VectorSource({ wrapX: false });
    var vector = new Vector({
        source: source,
        style: new Style({
            fill: new Fill({
                color: 'rgba(255, 255, 255, 0.2)'
            }),
            stroke: new Stroke({
                color: '#ffcc33',
                width: 2
            }),
            image: new Circle({
                radius: 7,
                fill: new Fill({
                    color: '#ffcc33'
                })
            })
        })
    });
    //将绘制层添加到地图容器中

    // 加载数据
    map.addLayer(TiandiMap_vec);
    map.addLayer(TiandiMap_cva);
    map.addLayer(vector);

    var modify = new Modify({source: source});
    map.addInteraction(modify);



    this.setState({
      TiandiMap_vec,
      TiandiMap_cva,
      vector,
      source
    });
  }



  addGraph = type => {
    let { map } = this.refs.map;
    let that = this
    let drawType = "None"
    switch (type) {
      case 1:
            drawType="Point"
        break;
      case 2:
           drawType="LineString"
        break;
      case 3:
            drawType="Polygon"
        break;
      case 4:
            drawType="Circle"
        break;
      case 5:
        break;
      case 6:
        break;
      default:
        break;
    }


    var draw = new Draw({
        //绘制层数据源
        source:that.state.source ,
        /** @type {ol.geom.GeometryType}几何图形类型 */
        type:drawType,
        // //几何信息变更时调用函数
        // geometryFunction: geometryFunction,
        //最大点数
        // maxPoints: 2
        //  设置后移动鼠标绘制图形
        // freehand: true
    });
    map.addInteraction(draw);
    let snap = new Snap({source: that.state.source});
    map.addInteraction(snap);

  };
  //添加点


  render() {
    return (
      <div>
        <button onClick={this.addGraph.bind(this, 1)}>绘制点</button>
        <button onClick={this.addGraph.bind(this, 2)}>绘制线</button>
        <button onClick={this.addGraph.bind(this, 3)}>绘制多边形</button>
        <button onClick={this.addGraph.bind(this, 4)}>绘制圆</button>
        {/* <button onClick={this.addGraph.bind(this, 5)}>添加矩形</button>
        <button onClick={this.addGraph.bind(this, 6)}>添加多边形</button> */}
        <Map ref="map" center={{ lon: 113.8, lat: 34.6 }} />;
      </div>
    );
  }
}

export default DrawEditGraph;
六、效果图






推荐了解热门学科

java培训 Python人工智能 Web前端培训 PHP培训
区块链培训 影视制作培训 C++培训 产品经理培训
UI设计培训 新媒体培训 产品经理培训 Linux运维
大数据培训 智能机器人软件开发




传智播客是一家致力于培养高素质软件开发人才的科技公司“黑马程序员”是传智播客旗下高端IT教育品牌。自“黑马程序员”成立以来,教学研发团队一直致力于打造精品课程资源,不断在产、学、研3个层面创新自己的执教理念与教学方针,并集中“黑马程序员”的优势力量,针对性地出版了计算机系列教材50多册,制作教学视频数+套,发表各类技术文章数百篇。

传智播客从未停止思考

传智播客副总裁毕向东在2019IT培训行业变革大会提到,“传智播客意识到企业的用人需求已经从初级程序员升级到中高级程序员,具备多领域、多行业项目经验的人才成为企业用人的首选。”

中级程序员和初级程序员的差别在哪里?
项目经验。毕向东表示,“中级程序员和初级程序员最大的差别在于中级程序员比初级程序员多了三四年的工作经验,从而多出了更多的项目经验。“为此,传智播客研究院引进曾在知名IT企业如阿里、IBM就职的高级技术专家,集中研发面向中高级程序员的课程,用以满足企业用人需求,尽快补全IT行业所需的人才缺口。

何为中高级程序员课程?

传智播客进行了定义。中高级程序员课程,是在当前主流的初级程序员课程的基础上,增加多领域多行业的含金量项目,从技术的广度和深度上进行拓展“我们希望用5年的时间,打造上百个高含金量的项目,覆盖主流的32个行业。”传智播客课程研发总监于洋表示。




黑马程序员热门视频教程【点击播放】

Python入门教程完整版(懂中文就能学会) 零起点打开Java世界的大门
C++| 匠心之作 从0到1入门学编程 PHP|零基础入门开发者编程核心技术
Web前端入门教程_Web前端html+css+JavaScript 软件测试入门到精通


在线咨询 我要报名
和我们在线交谈!