use*_*644 2 location google-chrome mocking android-emulator google-chrome-devtools
对于移动网络应用程序,我想模拟设备的位置移动。虽然可以使用 Chrome 开发者控制台中的传感器选项卡覆盖单个位置(请参阅: https: //developers.google.com/web/tools/chrome-devtools/device-mode/device-input-and-sensors)我想连续覆盖位置,例如每秒更新设备的位置。
是否有可能在 Chrome(或任何其他桌面浏览器)中实现这一目标?
我正在寻找类似于 Android 模拟器的解决方案,它允许重播记录的 GPS 轨迹(来自 GPX 或 KML 文件):
(参见: https: //developer.android.com/guide/topics/location/strategies.html#MockData)
DevTools 没有这方面的功能,但如果您碰巧正在使用,getCurrentPosition()
您几乎可以通过覆盖代码片段中的函数来重新创建它。
我想如果您正在使用(您可能正在使用),此工作流程将无法工作watchPosition()
,因为我相信这基本上是一个侦听器,当浏览器更新坐标时会被触发。无法更新浏览器的坐标。
但是,我将在下面记录工作流程,因为它可能对其他人有用。
navigator.geolocation.getCurrentPosition()
到目标坐标。因此,您可以将坐标和时间戳存储在 JSON 中(在代码片段中,或者仅使用 XHR / Fetch 从代码片段中获取 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);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2441 次 |
最近记录: |