Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌浏览器:通过控制台覆盖地理位置_Javascript_Google Chrome_Google Maps Api 3_Geolocation_Google Chrome Devtools - Fatal编程技术网

Javascript 谷歌浏览器:通过控制台覆盖地理位置

Javascript 谷歌浏览器:通过控制台覆盖地理位置,javascript,google-chrome,google-maps-api-3,geolocation,google-chrome-devtools,Javascript,Google Chrome,Google Maps Api 3,Geolocation,Google Chrome Devtools,所以我的问题有一点背景;我正在使用GoogleMapsDirections API编写一个简单的驾驶说明web应用程序,该应用程序为我提供了一个车位路径和文本说明 为了测试这个应用程序(不用开车),我需要模拟一个地理定位路径。Google Chrome支持通过sensors developer设置覆盖地理位置数据,该设置可以在一个坐标下正常工作 所以我的问题是-是否可以通过控制台(即javascript api)设置browsers navigator.geolocation数据,而不是手动更新

所以我的问题有一点背景;我正在使用GoogleMapsDirections API编写一个简单的驾驶说明web应用程序,该应用程序为我提供了一个车位路径和文本说明

为了测试这个应用程序(不用开车),我需要模拟一个地理定位路径。Google Chrome支持通过sensors developer设置覆盖地理位置数据,该设置可以在一个坐标下正常工作

所以我的问题是-是否可以通过控制台(即javascript api)设置browsers navigator.geolocation数据,而不是手动更新传感器设置菜单中的值

我知道在这种情况下,我可以使用浏览器地理位置数据以外的另一个输入源,使用LatLng的静态数组,或者覆盖浏览器navigator.geolocation.getCurrentPosition,但我认为覆盖传感器会更复杂


提前感谢。

一种方法是使用您自己的自定义函数覆盖函数
navigator.geolocation.getCurrentPosition
。在自定义函数中,您可以自定义纬度和经度的值

var customPosition = {};
customPosition.coords = {};
customPosition.coords.latitude = 41.89;
customPosition.coords.longitude = 2.89;

navigator.geolocation.getCurrentPosition = function(success, error){
    success(customPosition);

};

function success(position) {
    var latitude  = position.coords.latitude;
    var longitude = position.coords.longitude;

    console.log("latitude: " + latitude);
    console.log("longitude: " + longitude);
  }

您好,这很好,但如果我们尝试检查当前位置,它不会显示自定义位置检查我的代码JSFIDLE如果您能够帮助我,我将很高兴