Vue+Openlayer实现图形的拖动和旋转变形效果

 更新时间:2021年11月26日 16:30:34   作者:浩星  
Openlayer具有自己的扩展插件ol-ext,可以用来实现图形的拖拽、旋转、缩放、拉伸、移动等操作,本文将主要介绍通过Openlayer实现图形的拖动和旋转,需要的同学可以学习一下

前言

openlayer 是有他自己的扩展插件 ol-ext,我们这里用他来实现图形的操作:拖拽、旋转、缩放、拉伸、移动等等功能,以及他的监听事件,毕竟我们作图以后是需要保存数据给后端,存到数据库的。

相关资料

1、ol-ext官方地址:入口

2、ol-ext 对应的资料地址:入口

3、ol-ext 源码gitee地址:入口

4、openlayers 最新官网:入口

5、openlayers 官网api:入口

实现效果

旋转、拖动

图1、实现效果

图2、旋转效果

图3、左右移动效果

 实现步骤

1、vue中引入openlayers

npm i ol --save

附:npm下载指定版本命令,需要可以拿去

npm install --save-dev ol@6.9.0

2、vue中引入 openlayers的扩展包   ol-ext

npm install ol-ext --save

附:npm下载指定版本命令,需要可以拿去

npm install --save ol-ext@3.2.16

3、创建地图容器

<template>
  <div id="map" class="map"></div>
</template>

4、js中引入具体配置,根据你的具体改,我这里放的是我自己的

ol有关:

import "ol/ol.css";
import View from "ol/View";
import Map from "ol/Map";
import TileLayer from "ol/layer/Tile";
import Overlay from "ol/Overlay";
import XYZ from "ol/source/XYZ";
import { Vector as SourceVec ,Cluster,Vector as VectorSource } from "ol/source";
import { Feature } from "ol";
import { Vector as LayerVec , Vector as VectorLayer } from "ol/layer";
import { Point, LineString, Polygon } from "ol/geom";
 
import {
    Style,
    Icon,
    Fill,
    Stroke,
    Text,
    Circle as CircleStyle,
  } from "ol/style";
  import { OSM, TileArcGISRest } from "ol/source";

ol-ext有关:

import ExtTransform from 'ol-ext/interaction/Transform'

5、实现地图方法:

data() {
      return {
        map: null,
        center: [116.39702518856394, 39.918590567855425], //北京故宫的经纬度
        centerSize: 11.5,
        projection: "EPSG:4326",
 
    }
}
mounted() {
  this.initMap()
}
methods: {
      //初始化地图
      initMap() {
        //渲染地图
        var layers = [
          //深蓝色背景
          // new TileLayer({
          //   source: new XYZ({
          //     url:
          //       "https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
          //   }),
          // }),
          //初始化背景
          // new TileLayer({
          //   source: new OSM(),
          // })
          new TileLayer({
            title: "街道图",
            source: new XYZ({
              url: "http://localhost:8888/haoxing-map/sosomaps/roadmap/{z}/{x}/{y}.jpg",//zwh本地使用
            }),
          }),
        ];
 
        this.map = new Map({
          layers: layers,
          target: "map",
          view: new View({
            center: this.center,
            projection: this.projection,
            zoom: this.centerSize,
            maxZoom: 17,
            minZoom: 8,
          }),
        });
      },

6、地图上加上多边形数据

mounted() {
 this.initMap()
 this.createPolygon()
},
 methods: {    
 
    //创建多边形
    createPolygon() {
        //添加图层,并设置点范围
        const polygon = new Feature({
          geometry: new Polygon([
            [
              [116.39314093500519,40.0217660530101],
              [116.47762344990831,39.921746523871924],
              [116.33244947314951,39.89892653421418],
              [116.30623076162784,40.00185925352143],
            ]
          ]),
        })
        //设置样式
        polygon.setStyle(new Style({
          stroke: new Stroke({
            width: 4,
            color: [255, 0, 0, 1],
          }),
        }))
        //将图形加到地图上
        this.map.addLayer(new VectorLayer({
          source: new VectorSource({
            features: [polygon],
          }),
        }))
      },
 
}

7、地图上添加具体的操作方法和效果 

mounted() {
  this.initMap()
  this.createPolygon()
  this.onEdit()
},
//操作事件
onEdit() {
   const transform = new ExtTransform({
       enableRotatedTransform: false,
       hitTolerance: 2,
       translate: true, // 拖拽
       stretch: false, // 拉伸
       scale: true, // 缩放
       rotate: true, // 旋转
       translateFeature: false,
       noFlip: true,
       // layers: [],
    })
   this.map.addInteraction(transform)
 
 
  //开始事件
        transform.on(['rotatestart','translatestart'], function(e){
          // Rotation
          let startangle = e.feature.get('angle')||0;
          // Translation
          console.log(1111);
          console.log(startangle);
        });
  //旋转
        transform.on('rotating', function (e){
          console.log(2222);
          console.log("rotate: "+((e.angle*180/Math.PI -180)%360+180).toFixed(2));
          console.log(e);
        });
 //移动
        transform.on('translating', function (e){
          console.log(3333);
          console.log(e.delta);
          console.log(e);
 
        });
 //拖拽事件
        transform.on('scaling', function (e){
          console.log(4444);
          console.log(e.scale);
          console.log(e);
        });
  //事件结束
        transform.on(['rotateend', 'translateend', 'scaleend'], function (e) {
          console.log(5555);
        });
 
},

到此这篇关于Vue+Openlayer实现图形的拖动和旋转变形效果的文章就介绍到这了,更多相关Vue Openlayer内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue中router.beforeEach()的简单用法举例

    vue中router.beforeEach()的简单用法举例

    router.beforeEach()一般用来做一些进入页面的限制,比如没有登录,就不能进入某些页面,只有登录了之后才有权限查看某些页面,下面这篇文章主要给大家介绍了关于vue中router.beforeEach()的简单用法举例,需要的朋友可以参考下
    2023-01-01
  • vue实现图片拖动排序

    vue实现图片拖动排序

    这篇文章主要为大家详细介绍了vue实现图片拖动排序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • vue新玩法VueUse工具库具体用法@vueuse/core详解

    vue新玩法VueUse工具库具体用法@vueuse/core详解

    这篇文章主要介绍了vue新玩法VueUse-工具库@vueuse/core,VueUse不是Vue.use,它是一个基于 Composition API 的实用函数集合,下面是具体的一些用法,需要的朋友可以参考下
    2022-08-08
  • vue3项目中使用three.js的操作步骤

    vue3项目中使用three.js的操作步骤

    最近在学习Three.js相关的技术,恰逢Vue 3.0正式版也已推出,下面这篇文章主要给大家介绍了关于vue3项目中使用three.js的操作步骤,文中通过图文以及实例代码介绍的非常详细,需要的朋友可以参考下
    2023-01-01
  • vue-cli-service build 环境设置方式

    vue-cli-service build 环境设置方式

    这篇文章主要介绍了vue-cli-service build 环境设置方式,具有很好的参考价值,希望对大家有所帮助。
    2023-01-01
  • vue3+ts使用bus事件总线的示例代码

    vue3+ts使用bus事件总线的示例代码

    这篇文章主要介绍了vue3+ts使用bus事件总线,文中给大家提到了vue总线机制(bus)的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • 基于element-UI input等组件宽度修改方式

    基于element-UI input等组件宽度修改方式

    这篇文章主要介绍了基于element-UI input等组件宽度修改方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • vue配置生产环境.env.production与测试环境.env.development

    vue配置生产环境.env.production与测试环境.env.development

    这篇文章主要介绍了vue配置生产环境.env.production与测试环境.env.development方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • vue-music 使用better-scroll遇到轮播图不能自动轮播问题

    vue-music 使用better-scroll遇到轮播图不能自动轮播问题

    根据vue-music视频中slider组建的使用,当安装新版本的better-scroll,轮播组件,不能正常轮播。如何解决这个问题呢,下面小编给大家带来了vue-music 使用better-scroll遇到轮播图不能自动轮播问题,感兴趣的朋友一起看看吧
    2018-12-12
  • vue中keep-alive内置组件缓存的实例代码

    vue中keep-alive内置组件缓存的实例代码

    这篇文章主要介绍了vue中的keep-alive内置组件缓存,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04

最新评论