openlayer操作,贴图实现画框删除框

先安装依赖 我是用的cnpm

cnpm install ol --save-dev
<div id="map" class="map"></div>
import 'ol/ol.css'
import Map from 'ol/Map'
import View from 'ol/View'
import {getCenter} from 'ol/extent'
import ImageLayer from 'ol/layer/Image'
import Projection from 'ol/proj/Projection'
import Static from 'ol/source/ImageStatic'
import Draw, {createBox} from 'ol/interaction/Draw'
import {Vector as VectorLayer} from 'ol/layer'
import {Vector as VectorSource} from 'ol/source'
 extent: null,
      map: null,
      draw: null,
      source: null,
      imageLayer: null,
      vector: null,
      feature: null,
      coordinate: [],
      picturePath: '/symbol/fish1575335778547.jpg'
// 根据图片的大小去获取extent
let image = new Image()
      image.src = this.api + this.picturePath
      image.onload = () => {
        this.extent = [0, 0, image.width, image.height]
        // console.log(this.extent)
        var projection = new Projection({
          code: 'xkcd-image',
          units: 'pixels',
          extent: this.extent
        })
        this.source = new VectorSource({wrapX: false})
        this.vector = new VectorLayer({
          source: this.source
        })
        this.raster = new ImageLayer({
          source: new Static({
            url: 'https://imgs.xkcd.com/comics/online_communities.png',
            projection: projection,
            imageExtent: this.extent
          })
        })
        this.map = new Map({
          layers: [
            this.raster,
            this.vector
          ],
          target: 'map',
          view: new View({
            projection: projection,
            center: getCenter(this.extent),
            zoom: 1,
            maxZoom: 8
            // extent: this.extent
          })
        })
      }
addDrawInteraction () {
      this.draw = new Draw({
        source: this.source,
        type: 'Circle',
        geometryFunction: createBox()
      })
      this.draw.on('drawend', (e) => {
        console.log(e.feature.getGeometry().extent_)
        this.feature = e.feature
        this.map.removeInteraction(this.draw) // 取消连续画框的操作
      })
      this.map.addInteraction(this.draw)
    },
    // 点击添加的时候再在图上画框
    add () {
      if (this.feature) {
        this.source.removeFeature(this.feature) // 删除之前标记的内容
      }
      this.addDrawInteraction()
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值