vue+openalyers实时显示鼠标坐标组件

在这里插入图片描述
封装了一个实时显示鼠标坐标的组件供大家参考
组件代码:

<template>
    <div>
        <div id="pointOverlay">
            <span>x:{{ coordinations[0] }} y:{{ coordinations[1] }}</span>
        </div>
    </div>
</template>

<script >
import Overlay from "ol/Overlay.js";
export default {
    props: [ 'map', 'show'],
    data() {
        return {
            mousePositionOverlay: null,
            coordinations:[]
        }
    },
    mounted() {
        let element = document.getElementById("pointOverlay")
        this.mousePositionOverlay = new Overlay({
            element: element,
            position: null,
            positioning: "bottom-center"
        })
    },
    methods: {
       pointermoveHandler(event) {
            this.coordinations = [event.coordinate[0].toFixed(4), event.coordinate[1].toFixed(4)]
            this.mousePositionOverlay.setPosition(event.coordinate)  
        }
    },
    watch: {
        show: {
            immediate:true,
            handler(newVal) {
                let _this = this
                if (newVal) {
                    this.map.addOverlay(_this.mousePositionOverlay)
                    this.map.on('pointermove', _this.pointermoveHandler)
                } else {
                    this.map.removeOverlay(this.mousePositionOverlay)
                    this.map.un('pointermove', _this.pointermoveHandler)
                }
            }
        }
    }


}

</script>
<style scoped lang="less">
#pointOverlay {
    width: 230px;
    height: 30px;
    border: 2px solid #E6A23C;
    border-radius: 2px;
    text-align: center;
    line-height: 30px;
    color: white;
    margin-bottom: 5px;
    backdrop-filter: blur(5px);
    background-color: rgba(255, 255, 255, 0.5);
}
</style>

使用的时候只需要传入两个属性map(当前的地图对象),show(控制组件显示隐藏的变量)
引入:这里用自己的路径

import MousePosition from "../../PublicModal/MousePosition.vue";

注册:(这里使用的是vue的模式哈,vue3应该是不需要注册直接使用的):

  components: {
    MousePosition
  },

使用:

//map为当前实例化的地图, isShow控制组件的显示与隐藏
 <MousePosition :map="map" :show="isShow"></MousePosition>

感谢您的阅读,如果有用的话可以点个赞吗?谢谢大佬!嘿嘿嘿————

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值