【问题标题】:Google Chrome: Override geolocation via console谷歌浏览器:通过控制台覆盖地理位置
【发布时间】:2017-12-10 12:44:42
【问题描述】:

所以我的问题有一点背景;我正在使用 Google Maps Directions API 编写一个简单的驾驶说明 Web 应用程序,它为我提供了 LatLng 路径以及文本说明。

为了测试这个应用程序(不开车四处走动),我需要模拟一个地理定位路径。谷歌浏览器支持通过传感器开发者设置覆盖地理定位数据,这在一次使用一个坐标时可以正常工作。

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

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

提前致谢。

【问题讨论】:

    标签: javascript google-chrome google-maps-api-3 geolocation google-chrome-devtools


    【解决方案1】:

    一种方法是用您自己的自定义函数覆盖函数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);
      }
    

    【讨论】:

    • 嗨,这很好,但是如果我们尝试查看当前位置,它不会显示自定义位置,请检查我的代码 jsfiddle here 如果你能帮助我,我会很高兴
    猜你喜欢
    • 1970-01-01
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 2011-01-20
    • 1970-01-01
    相关资源
    最近更新 更多