Vue+Openlayer封装拾取坐标组件-LjOlPickCoordinate

点击地图,可复制点击处坐标

复制功能使用vue-clipboard2,参考:Vue实现复制粘贴-基于vue-clipboard2_李疆的博客-CSDN博客_vuejs 实现复制

首先下载 复制文本组件vue-clipboard2

cnpm i -S vue-clipboard2

main.js中引入

import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)

基本使用:

<template>
  <div>
    <div id="map" style="width: 100vw; height: 100vh"></div>
    <LjOlPickCoordinate
      :map="map"
      style="position: fixed; top: 200px; left: 800px"
    ></LjOlPickCoordinate>
  </div>
</template>
 
<script>
import "ol/ol.css";
import { Map, View } from "ol";
import { OSM } from "ol/source";
import { Tile as TileLayer } from "ol/layer";
import LjOlPickCoordinate from "@/components/LjOlPickCoordinate/index.vue";

export default {
  components: { LjOlPickCoordinate },

  data() {
    return {
      map: {},
    };
  },
  mounted() {
    this.initMap();
  },
  methods: {
    // 初始化地图
    initMap() {
      this.map = new Map({
        target: "map",
        layers: [
          new TileLayer({
            source: new OSM(),
          }),
        ],
        view: new View({
          projection: "EPSG:4326",
          center: [104.29806, 30.5263],
          zoom: 18,
        }),
      });
    },
  },
};
</script>

LjOlPickCoordinate/index.vue:

<!--LjOlPickCoordinate-->
<template>
  <div>
    <div
      v-if="divCoordinate.showCoordinateDiv"
      :style="{ left: divCoordinate.left, top: divCoordinate.top }"
      style="position: fixed; color: white; font-size: 18px"
    >
      {{ divCoordinate.coordinateMouse }}
    </div>
    <el-tooltip
      class="item"
      effect="dark"
      :content="pickCoordinateTip"
      placement="right"
    >
      <LjButton type="primary" size="mini" @click="pickCoordinate()"
        ><i class="el-icon-map-location"
      /></LjButton>
    </el-tooltip>
  </div>
</template>

<script>
import LjButton from "@/components/LjButton/index.vue";
import { unByKey } from "ol/Observable"; //移除事件

export default {
  name: "LjOlPickCoordinate",
  components: { LjButton },
  props: {
    map: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      isPickCoordinate: false, //是否拾取坐标
      event_pickCoordinate_click: {}, //拾取坐标事件 click
      pickCoordinateTip: "点击拾取坐标",
      event_pickCoordinate_pointermove: {}, //拾取坐标事件 pointermove

      divCoordinate: {
        showCoordinateDiv: false,
        left: "",
        top: "",
        coordinateMouse: [], //鼠标坐标
      },
    };
  },
  computed: {},
  created() {},
  mounted() {},
  filters: {},
  methods: {
    //拾取坐标
    pickCoordinate() {
      this.isPickCoordinate = !this.isPickCoordinate;
      if (this.isPickCoordinate) {
        this.pickCoordinateTip = "取消坐标拾取";

        this.event_pickCoordinate_click = this.map.on("click", async (e) => {
          let { text } = await this.$copyText(e.coordinate);
          this.$message.success(`坐标复制成功:${text}`);
        });

        this.event_pickCoordinate_pointermove = this.map.on(
          "pointermove",
          (e) => {
            document.getElementById("app").style.cursor = "crosshair";
            this.divCoordinate.coordinateMouse = e.coordinate;

            this.divCoordinate.showCoordinateDiv = true;
            this.divCoordinate.left = e.pixel[0] + 40 + "px";
            this.divCoordinate.top = e.pixel[1] - 50 + "px";
          }
        );
      } else {
        this.pickCoordinateTip = "点击拾取坐标";
        document.getElementById("app").style.cursor = "default";

        unByKey(this.event_pickCoordinate_click);
        unByKey(this.event_pickCoordinate_pointermove);

        this.divCoordinate.showCoordinateDiv = false;
      }
    },
  },
  watch: {},
};
</script>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值