当前位置:   article > 正文

vue+openlayer实现:拖拽、旋转、缩放、拉伸、移动等功能以及对应的监听事件_vue中openlayes图同比例缩放属性

vue中openlayes图同比例缩放属性

前言:

      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、创建地图容器

  1. <template>
  2. <div id="map" class="map"></div>
  3. </template>

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

ol有关:

  1. import "ol/ol.css";
  2. import View from "ol/View";
  3. import Map from "ol/Map";
  4. import TileLayer from "ol/layer/Tile";
  5. import Overlay from "ol/Overlay";
  6. import XYZ from "ol/source/XYZ";
  7. import { Vector as SourceVec ,Cluster,Vector as VectorSource } from "ol/source";
  8. import { Feature } from "ol";
  9. import { Vector as LayerVec , Vector as VectorLayer } from "ol/layer";
  10. import { Point, LineString, Polygon } from "ol/geom";
  11. import {
  12. Style,
  13. Icon,
  14. Fill,
  15. Stroke,
  16. Text,
  17. Circle as CircleStyle,
  18. } from "ol/style";
  19. import { OSM, TileArcGISRest } from "ol/source";

ol-ext有关:

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

5、实现地图方法:

  1. data() {
  2. return {
  3. map: null,
  4. center: [116.39702518856394, 39.918590567855425], //北京故宫的经纬度
  5. centerSize: 11.5,
  6. projection: "EPSG:4326",
  7. }
  8. }
  1. mounted() {
  2. this.initMap()
  3. }
  1. methods: {
  2. //初始化地图
  3. initMap() {
  4. //渲染地图
  5. var layers = [
  6. //深蓝色背景
  7. // new TileLayer({
  8. // source: new XYZ({
  9. // url:
  10. // "https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
  11. // }),
  12. // }),
  13. //初始化背景
  14. // new TileLayer({
  15. // source: new OSM(),
  16. // })
  17. new TileLayer({
  18. title: "街道图",
  19. source: new XYZ({
  20. url: "http://localhost:8888/haoxing-map/sosomaps/roadmap/{z}/{x}/{y}.jpg",//zwh本地使用
  21. }),
  22. }),
  23. ];
  24. this.map = new Map({
  25. layers: layers,
  26. target: "map",
  27. view: new View({
  28. center: this.center,
  29. projection: this.projection,
  30. zoom: this.centerSize,
  31. maxZoom: 17,
  32. minZoom: 8,
  33. }),
  34. });
  35. },

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

  1. mounted() {
  2. this.initMap()
  3. this.createPolygon()
  4. },
  1. methods: {
  2. //创建多边形
  3. createPolygon() {
  4. //添加图层,并设置点范围
  5. const polygon = new Feature({
  6. geometry: new Polygon([
  7. [
  8. [116.39314093500519,40.0217660530101],
  9. [116.47762344990831,39.921746523871924],
  10. [116.33244947314951,39.89892653421418],
  11. [116.30623076162784,40.00185925352143],
  12. ]
  13. ]),
  14. })
  15. //设置样式
  16. polygon.setStyle(new Style({
  17. stroke: new Stroke({
  18. width: 4,
  19. color: [255, 0, 0, 1],
  20. }),
  21. }))
  22. //将图形加到地图上
  23. this.map.addLayer(new VectorLayer({
  24. source: new VectorSource({
  25. features: [polygon],
  26. }),
  27. }))
  28. },
  29. }

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

  1. mounted() {
  2. this.initMap()
  3. this.createPolygon()
  4. this.onEdit()
  5. },
  1. //操作事件
  2. onEdit() {
  3. const transform = new ExtTransform({
  4. enableRotatedTransform: false,
  5. hitTolerance: 2,
  6. translate: true, // 拖拽
  7. stretch: false, // 拉伸
  8. scale: true, // 缩放
  9. rotate: true, // 旋转
  10. translateFeature: false,
  11. noFlip: true,
  12. // layers: [],
  13. })
  14. this.map.addInteraction(transform)
  15. //开始事件
  16. transform.on(['rotatestart','translatestart'], function(e){
  17. // Rotation
  18. let startangle = e.feature.get('angle')||0;
  19. // Translation
  20. console.log(1111);
  21. console.log(startangle);
  22. });
  23. //旋转
  24. transform.on('rotating', function (e){
  25. console.log(2222);
  26. console.log("rotate: "+((e.angle*180/Math.PI -180)%360+180).toFixed(2));
  27. console.log(e);
  28. });
  29. //移动
  30. transform.on('translating', function (e){
  31. console.log(3333);
  32. console.log(e.delta);
  33. console.log(e);
  34. });
  35. //拖拽事件
  36. transform.on('scaling', function (e){
  37. console.log(4444);
  38. console.log(e.scale);
  39. console.log(e);
  40. });
  41. //事件结束
  42. transform.on(['rotateend', 'translateend', 'scaleend'], function (e) {
  43. console.log(5555);
  44. });
  45. },

到此就结束了!有问题欢迎评论区一起讨论

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/109989
推荐阅读
相关标签
  

闽ICP备14008679号