ol自定义指北针

<template>
<div class="sindagis-map-compass">
    <span class="left" @click="leftClick"></span>
    <span class="center" @click="recoveryClick" :style="{transform: `rotate(${rotate}deg)`}"></span>
    <span class="right" @click="rightClick"></span>
</div>
</template>
 
<script>
import { easeOut } from 'ol/easing'
export default {
    name: "MapCompass",
    created() {
    },
    data(){
        return {
            rotate: 0,
            currentDir: ''
        }
    },
    beforeDestroy() {},
    methods: {
        leftClick(){
            var view = this.map.getView();
            var center = this.map.getView().getCenter();
            var rotation = this.map.getView().getRotation();
            rotation = rotation - Math.PI/2
            this.rotate -= 90
            view.animate({
                center: center,                                            //旋转中心点
                rotation: rotation,                        
                easing: easeOut                               //旋转速度
            }) 
            this.currentDir = 'left'
        },
        recoveryClick(){
            var view = this.map.getView();
            var center = this.map.getView().getCenter();
            let a = this.rotate % 360 // -270
            if(a != 0){ // 复位
                let dis = 360 - Math.abs(a)
                let dis1 = Math.abs(a)
                dis = dis > dis1 ? dis1 : dis
                if(dis == 180) dis = -180
                this.rotate = (this.rotate + dis) % 360 == 0 ? this.rotate + dis : this.rotate - dis
            }
            view.animate({
                center: center,
                rotation: 0,
                easing: easeOut
            })
        },
        rightClick(){
            var view = this.map.getView();
            var center = this.map.getView().getCenter();
            var rotation = this.map.getView().getRotation();
            rotation = rotation + Math.PI/2
            this.rotate += 90
            view.animate({
                center: center,
                rotation: rotation,
                easing: easeOut
            })
            this.currentDir = 'right'
        },
    }
}
</script>
 
<style lang="scss">
.sindagis-map-compass{
    position: absolute;
    z-index: 5;
    bottom: 60px;
    right: 0px;
    width: 52px;
    height: 54px;
    background: url('../../assets/img/earth-navi-control-pc4.png') 0% 0% / 266px no-repeat;
    .left, .center, .right{
        position: absolute;
        outline: none;
        border: none;
        cursor: pointer;
        opacity: 1;
    }
    .left{
        background: url('../../assets/img/earth-navi-control-pc4.png') -75px -5px / 266px no-repeat;
        left: 2px;
        top: 5px;
        z-index: 1;
        width: 15px;
        height: 42px;
        &:hover{
           background: url('../../assets/img/earth-navi-control-pc4.png') -89px -5px / 266px no-repeat; 
        }
    }
    .center{
        background: url('../../assets/img/earth-navi-control-pc4.png') -56px -4px / 266px no-repeat;
        left: 19px;
        top: 4px;
        width: 14px;
        height: 44px;
        transform: rotate(0deg);
        transition: 1s;
    }
    .right{
        background: url('../../assets/img/earth-navi-control-pc4.png') -75px -5px / 266px no-repeat;
        right: 2px;
        top: 5px;
        z-index: 1;
        width: 15px;
        height: 42px;
        transform: scaleX(-1);
        &:hover{
           background: url('../../assets/img/earth-navi-control-pc4.png') -89px -5px / 266px no-repeat;
        }
    }
}
</style>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值