Vue+Openlayer realizes the dragging and rotation deformation effect of graphics

Vue+Openlayer realizes the dragging and rotation deformation effect of graphics

Preface

Openlayer has its own extension plug-in ol-ext, which we use here to implement graphics operations: drag, rotate, zoom, stretch, move, etc., as well as its event monitoring. After all, after drawing, we need to save the data to the backend and store it in the database.

Related Materials

1. ol-ext official address: entrance

2. ol-ext corresponding data address: entry

3.ol-ext source code gitee address: entry

4. Openlayers latest official website: entrance

5. Openlayers official website API: entry

Achieve results

Rotate, drag

Figure 1: Implementation effect

Figure 2: Rotation effect

Figure 3: Left and right movement effect

Implementation steps

1. Introduce openlayers into vue

npm i ol --save

Attachment: npm downloads the specified version command, you can take it if you need it

npm install --save-dev [email protected]

2. Introduce the openlayers extension package ol-ext into vue

npm install ol-ext --save

Attachment: npm downloads the specified version command, you can take it if you need it

npm install --save [email protected]

3. Create a map container

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

4. Introduce specific configuration in js, according to your specific changes, I put my own here

ol related:

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 related:

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

5. Implement the map method:

data() {
      return {
        map: null,
        center: [116.39702518856394, 39.918590567855425], //The longitude and latitude of the Forbidden City in Beijing centerSize: 11.5,
        projection: "EPSG:4326",
 
    }
}
mounted() {
  this.initMap()
}
methods: {
      //Initialize the map initMap() {
        // Render the map var layers = [
          //Dark blue background// new TileLayer({
          // source: new XYZ({
          // url:
          // "https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
          // }),
          // }),
          // Initialize background // new TileLayer({
          // source: new OSM(),
          // })
          new TileLayer({
            title: "Street Map",
            source: new XYZ({
              url: "http://localhost:8888/haoxing-map/sosomaps/roadmap/{z}/{x}/{y}.jpg", //zwh local use}),
          }),
        ];
 
        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. Add polygon data to the map

mounted() {
 this.initMap()
 this.createPolygon()
},
 methods: {    
 
    //Create a polygon createPolygon() {
        //Add a layer and set the point range const polygon = new Feature({
          geometry: new Polygon([
            [
              [116.39314093500519,40.0217660530101],
              [116.47762344990831,39.921746523871924],
              [116.33244947314951,39.89892653421418],
              [116.30623076162784,40.00185925352143],
            ]
          ]),
        })
        //Set style polygon.setStyle(new Style({
          stroke: new Stroke({
            width: 4,
            color: [255, 0, 0, 1],
          }),
        }))
        //Add the graphics to the map this.map.addLayer(new VectorLayer({
          source: new VectorSource({
            features: [polygon],
          }),
        }))
      },
 
}

7. Add specific operation methods and effects to the map 

mounted() {
  this.initMap()
  this.createPolygon()
  this.onEdit()
},
//Operation event onEdit() {
   const transform = new ExtTransform({
       enableRotatedTransform: false,
       hitTolerance: 2,
       translate: true, // drag stretch: false, // stretch scale: true, // scale rotate: true, // rotate translateFeature: false,
       noFlip: true,
       // layers: [],
    })
   this.map.addInteraction(transform)
 
 
  //Start event transform.on(['rotatestart','translatestart'], function(e){
          // Rotation
          let startangle = e.feature.get('angle')||0;
          // Translation
          console.log(1111);
          console.log(startangle);
        });
  //Rotation transform.on('rotating', function (e) {
          console.log(2222);
          console.log("rotate: "+((e.angle*180/Math.PI -180)%360+180).toFixed(2));
          console.log(e);
        });
 //Move transform.on('translating', function (e){
          console.log(3333);
          console.log(e.delta);
          console.log(e);
 
        });
 //Drag event transform.on('scaling', function (e){
          console.log(4444);
          console.log(e.scale);
          console.log(e);
        });
  //Event end transform.on(['rotateend', 'translateend', 'scaleend'], function (e) {
          console.log(5555);
        });
 
},

This is the end of this article about Vue+Openlayer to achieve the dragging and rotation deformation effects of graphics. For more related Vue Openlayer content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue uses openlayers to load Tiandi Map and Amap
  • Realize map aggregation and scattering effects based on vue+openlayer
  • Openlayers draws administrative divisions in the Vue project
  • Vue+Openlayers custom track animation
  • Vue uses openlayers to implement moving point animation

<<:  Detailed explanation of MySQL DEFINER usage

>>:  Linux Operation and Maintenance Basic System Disk Management Tutorial

Recommend

The use of MySQL triggers and what to pay attention to

Table of contents About Triggers Use of triggers ...

Two ways to implement text stroke in CSS3 (summary)

question Recently I encountered a requirement to ...

An in-depth introduction to React refs

1. What is Refs is called Resilient File System (...

Detailed instructions for installing Jenkins on Ubuntu 16.04

1. Prerequisites JDK has been installed echo $PAT...

How to install and configure SSH service in Ubuntu 18.04

Install ssh tool 1. Open the terminal and type th...

How to automatically delete records before a specified time in Mysql

About Event: MySQL 5.1 began to introduce the con...

JavaScript Basics Series: Functions and Methods

Table of contents 1. The difference between funct...

Summary of 6 Linux log viewing methods

As a backend programmer, you deal with Linux in m...

A brief discussion on Mysql specified order sorting query

Recently, I have been working on a large-screen d...

Vue implements table paging function

This article example shares the specific code of ...

A detailed introduction to JavaScript primitive values ​​and wrapper objects

Table of contents Preface text Primitive types Pr...

Understanding flex-grow, flex-shrink, flex-basis and nine-grid layout

1. flex-grow, flex-shrink, flex-basis properties ...