【问题标题】:How to stop mapbox-gl.js tracking my mobile compass如何停止 mapbox-gl.js 跟踪我的移动指南针
【发布时间】:2017-03-17 17:05:27
【问题描述】:

当我在手机上使用我的 mapbox-gl.js 地图时,它会在我转动手机以保持北方时不断旋转。我怎样才能阻止这个?我确实希望继续能够通过触摸手势旋转地图...

这里是api

【问题讨论】:

  • 你是说手机每次旋转地图都会旋转?这听起来很奇怪,当然不是预期的行为,也不是我所见过的。可以发网址吗?

标签: javascript mapbox-gl-js


【解决方案1】:

嗯...,对不起,方位跟踪目前不在 mapbox-gl.js api 中。这是“我自己的”代码。我从某个地方调整过的一个 sn-p 忘记了...:

  const easing = t => t * (2 - t)

  if (window.DeviceOrientationEvent) {
    window.addEventListener('deviceorientation', event => {
      let compassdir
      if (event.webkitCompassHeading) {
        // Apple works only with this, alpha doesn't work
        compassdir = event.webkitCompassHeading
      }
      else {
        compassdir = event.alpha
      }
      myMap.easeTo({
        bearing: 360 - compassdir,
        easing: easing
      })
    })
  }
  else {
    alert("Sorry, your browser doesn't support Device Orientation")
  }

【讨论】:

  • 仅供参考,这实际上是 Mapbox GL 内置的,使用 setMyBearingTrackingMode 函数和 COMPASS 值。
猜你喜欢
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多