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

Example code for using HTML ul and li tags to display images

Copy the following code to the code area of ​​Drea...

Summary of the differences between get and post requests in Vue

The operating environment of this tutorial: Windo...

MySQL 8.0.13 download and installation tutorial with pictures and text

MySQL is the most commonly used database. You mus...

js to upload pictures to the server

This article example shares the specific code of ...

Detailed Example of JavaScript Array Methods

Table of contents Introduction Creating an Array ...

A permanent solution to MYSQL's inability to recognize Chinese

In most cases, MySQL does not support Chinese whe...

Detailed explanation of the getBoundingClientRect() method in js

1. getBoundingClientRect() Analysis The getBoundi...

Database query optimization: subquery optimization

1. Case Take all employees who are not the head o...

Front-end development must learn to understand HTML tags every day (1)

2.1 Semanticization makes your web pages better u...

JavaScript dynamically generates a table with row deletion function

This article example shares the specific code of ...

The use of FrameLayout in six layouts

Preface In the last issue, we explained LinearLay...

Detailed explanation of several ways to create a top-left triangle in CSS

Today we will introduce several ways to use CSS t...

Graphical explanation of the underlying principle of JavaScript scope chain

Table of contents Preface Scope 1. What is scope?...