【问题标题】:Continuously emulate GPS Locations on Chrome在 Chrome 上持续模拟 GPS 位置
【发布时间】:2017-09-06 13:31:06
【问题描述】:

对于移动网络应用程序,我想模拟设备的位置移动。虽然可以使用 Chrome 开发者控制台中的传感器选项卡覆盖单个位置(请参阅:https://developers.google.com/web/tools/chrome-devtools/device-mode/device-input-and-sensors),但我想连续覆盖该位置,例如每秒更新设备的位置。

是否有可能在 Chrome(或任何其他桌面浏览器)中实现这一点?

我正在寻找一种类似于 Android Emulator 的解决方案,它允许重播记录的 GPS 轨迹(来自 GPX 或 KML 文件):

(参见:https://developer.android.com/guide/topics/location/strategies.html#MockData

【问题讨论】:

    标签: google-chrome android-emulator mocking location google-chrome-devtools


    【解决方案1】:

    DevTools 没有这方面的功能,但如果您碰巧使用getCurrentPosition(),您几乎可以通过覆盖 sn-p 中的功能来重新创建它。

    如果您使用watchPosition()(您可能是),我想这个工作流程将不起作用,因为我相信这基本上是一个在浏览器更新坐标时被触发的侦听器。无法更新浏览器的坐标。

    但是,我将在 b/c 下方记录工作流程,它可能对其他人有用。

    1. 将您的脚本存储在snippet 中。
    2. Override navigator.geolocation.getCurrentPosition() 到目标坐标。

    因此,您可以将坐标和时间戳存储在 JSON 中(在 sn-p 中,或者使用 XHR / Fetch 从 sn-p 获取 JSON),然后使用 setTimeout() 更新坐标指定时间。

    var history = [
      {
        time: 1000,
        coords: ...
      },
      {
        time: 3000,
        coords: ...
      }
    ];
    
    for (var i = 0; i < history.length; i++) {
      setTimeout(function() {
        navigator.geolocation.getCurrentPosition = function(success, failure) { 
          success({ 
            coords: history[i].coords,
            timestamp: Date.now()
          }); 
      }, history[i].time);
    }
    

    【讨论】:

    • 当 maps.google.com 在一个有几个 gps 位置的选项卡中打开时,刚刚尝试了这个 sn-p。不幸的是,地图上的位置标记只有在我手动重新加载页面时才会刷新(一次)。我认为这是因为 google-maps 使用watchPosition() 不断更新位置(而不是getCurrentPosition())?正如你所提到的,看起来真的没有办法持续更新浏览器的位置:(无论如何谢谢你分享你的工作流程。
    猜你喜欢
    • 1970-01-01
    • 2016-08-13
    • 2017-01-05
    • 2018-04-26
    • 2020-10-14
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多