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

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

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

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

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

if(navigator.geolocation) {

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

}

最佳答案

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

关于javascript - 如何使用WatchPosition()函数Geolocation来观看其他坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17518879/

相关文章:

javascript - react 子 JSX 元素不从父级继承

javascript - 如何根据类别为 ng-map 标记着色

php - 使用 PHP 检测 HTML5 地理定位支持

javascript - 所选选项的 Angular ng-init 不起作用

javascript - Highcharts 导出数据 : Show notes in the data table

javascript - 选择 map 控件外部的 Google map 多边形并修改属性

javascript - 谷歌地图 API v3 : Initial View is Fine but Gray Box with No Map if View is changed

javascript - MongoDB/Mongoose - 与 geoNear 和子文档的聚合

javascript - Google Maps API v3 Geolocation 在 Google Chrome 中不起作用

javascript - 最好的办法 :not in jQuery?