如何使用WatchPosition()函数Geolocation来观察其他坐标

How to use WatchPosition() function Geolocation to watch other coords

本文关键字:观察 其他 坐标 Geolocation 函数 何使用 WatchPosition      更新时间:2023-09-26

你好,我想知道是否有一种方法可以为WatchPosition()函数提供从坐标数组提供的特定Lat/Lon。

我在创建一个使用默认coords之类的回调函数时遇到了问题。lat coords。长

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

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

}

我需要做同样的事情,所以我写了一个测试脚本,用mock对象替换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数组。