【问题标题】:How to use WatchPosition() function Geolocation to watch other coords如何使用 WatchPosition() 函数 Geolocation 观看其他坐标
【发布时间】:2013-07-08 03:43:31
【问题描述】:

您好,我想知道是否有一种方法可以提供由坐标数组提供的 WatchPosition() 函数特定的纬度/经度。

我在创建使用默认坐标之类的回调函数时遇到问题。纬度坐标。长

换句话说,我想创建某种可以以类似方式使用但提供我的任意坐标集的函数,以便 watchPosition() 可以在循环通过数组和不同位置时更新位置设置好了。

if(navigator.geolocation) {

    navigator.geolocation.watchPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);

}

【问题讨论】:

    标签: javascript google-maps-api-3 geolocation w3c-geolocation


    【解决方案1】:

    我需要做同样的事情,所以我编写了一个测试脚本来用一个模拟对象替换 navigator.geolocation 对象。这是我的代码:

    // Replace real geolocation with mock geolocation
    delete navigator.geolocation;    
    navigator.geolocation = {
      isMock: true,
      paused: true,
      delay: 100,
      shouldFail: false,
      failsAt: -1,
      unFailsAt: -1,
      errorMessage: "There was an error retrieving the position!",
      currentTimeout: -1,
      lastPosReturned: 0,
      overrideAccuracy: 0, // accuracy in m to return for all positions (overrides any existing accuracies defined in the waypoints)
      useOverrideAccuracy: false, // Whether to override acurracies defined in the waypoints with the above override accuracy      
    
      _geoCall: function(method, success, error, repeat) {
    
          return this.currentTimeout = window[method].call(null, __bind(function() {
    
            var nextPos;
    
            if(!this.paused && this.lastPosReturned < this.waypoints.length - 1){
                nextPos = this.lastPosReturned++;               
            }else{
                this.lastPosReturned = nextPos = 0;
            }
    
    
            if(!this.shouldFail && nextPos == this.failsAt) this.shouldFail = true;
            if(this.shouldFail && nextPos == this.unFailsAt) this.shouldFail = false;
    
            if(repeat) this._geoCall("setTimeout", success, error, true);
    
            if (this.shouldFail && (error != null)) {               
                return error(this.errorMessage);
            }
            else if (success != null && !this.paused) {                
                var result = this.waypoints[nextPos];
                result.isMock = true;
                if(this.useOverrideAccuracy) result.coords.accuracy = this.overrideAccuracy;               
                success(result);
                return nextPos;
            }
          }, this), this.delay);
    
      },
      getCurrentPosition: function(success, error) {
        return this._geoCall("setTimeout", success, error, false);
      },
      watchPosition: function(success, error) {
        this._geoCall("setTimeout", success, error, true);
        return this.currentTimeout;
      },
      clearWatch: function(id) {
        return clearInterval(this.currentTimeout);
      },
      waypoints: []
    };
    

    然后我只需要用位置对象填充 navigator.geolocation.waypoints 数组。

    【讨论】:

    • 我想使用 watchPosition 跟踪我自己的位置,然后使用相同的函数观察其他人的位置,前提是他们的坐标是从数组提供的。
    • 所以您想同时接收“真实”位置更新和模拟位置更新?您可以使用我上面的代码进行模拟位置更新,但将其重命名为 navigator.mockGeolocation。然后您将能够同时观看真实位置(navigator.geolocation.watchPosition)和模拟位置(navigator.mockGeolocation.watchPosition)。
    • 酷,很高兴你让它工作了 :-) 请接受我对这个问题的回答,以便其他人知道它已经得到回答 :-)
    • 单击此答案左上角附近的向上/向下箭头下方的“勾号”。当你将鼠标悬停在它上面时,我会变绿。您也可以单击向上箭头 ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 2017-02-03
    • 2019-12-13
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    相关资源
    最近更新 更多