高德地图强制横屏后(旋转90deg)在手机上拖动方向问题(vue)

大屏需要在手机端横屏展示,强制横屏后,地图的拖动方向也相对正常的方向旋转90度

代码片段

html部分

<div
      id="mapInit"
      @touchstart.prevent="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd"
      :style="{width:component.width+'px',height:component.height+'px'}"
    ></div>

js部分

 	handleTouchEnd() {
      this.pre = "";
      this.count = 0;
    },
    handleTouchStart(e) {
      this.pre = this.getLnglat(e.touches[0].clientY, e.touches[0].clientX);
      // console.log('start',this.pre);
    },
    handleTouchMove(e) {
      // return
      // const { windowWidth, windowHeight } = uni.getSystemInfoSync();
      this.count = this.count + 1;
      if (this.count % 3 != 0) {
        return;
      }
      // 构造成 Pixel 对象后传入
      var lnglat = this.getLnglat(e.touches[0].clientY, e.touches[0].clientX); // 获得 LngLat 对象
      let lng = this.mapObj.getCenter().lng - (lnglat.lng - this.pre.lng) / 2;
      let lat = this.mapObj.getCenter().lat + (lnglat.lat - this.pre.lat) / 2;
      this.mapObj.setCenter([lng, lat]);
    },
    getLnglat(x, y) {
      var pixel = new AMap.Pixel(x, y);
      return this.mapObj.containerToLngLat(pixel);
    },
    mapInit() {
      const { isMobile } = this;
      const { zoom, center, styleId } = this.option;
      let cen = JSON.parse(center);
      const map = new AMap.Map('mapInit', {
        center: cen,
        resizeEnable: true,
        // viewMode: "3D",
        zoom,
        dragEnable: !isMobile(),//手机端设置为false
        // dragEnable: false,
        showLabel: true,
        touchZoomCenter: 1,
        mapStyle: `amap://styles/${styleId}`,
      });
      // map.setFitView();
      this.mapObj = map;
      this.setMarker();
      if (this.eventList.length !== 0) {
        this.setEventMarker();
      }
    },
    //判断手机端还是PC端
    isMobile() {
      if (
        navigator.userAgent.match(
          /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
        )
      ) {
        return true;
      } else {
        return false;
      }
    },

可以通过调节(lnglat.lng - this.pre.lng) / 2中的2来改变移动的步数大小,数字越大移动越小;或者也可以调节this.count % 3

转载于文章https://blog.csdn.net/rubylint/article/details/106068247。非常感谢啦啦

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值