javascript - JS - Promise + geolocation.watchPosition

标签 javascript geolocation promise

我尝试将 JavaScript Promise 与地理定位结合使用,但无法使其与 geolocation.watchPosition 一起正常工作,then 子句仅被调用一次:

function Geolocation() {
  this._options = {
    enableHighAccuracy: true, 
    maximumAge        : 10000, 
    timeout           : 7000
  }
}

Geolocation.prototype = {
  get watchID() { return this._watchID; },
  set watchID(watchID) { this._watchID = watchID; },
  get options() { return this._options; },
  // hasCapability: function() { return "geolocation" in navigator; },
  _promise: function(promise) {
    var geolocation = this;
    if (promise == "getPosition")
      return new Promise(function(ok, err) {
        navigator.geolocation.getCurrentPosition(
          ok.bind(geolocation), err.bind(geolocation),
          geolocation.options
        );
      });
    else if (promise == "watchPosition")
      return new Promise(function(ok, err) {
        geolocation.watchID = navigator.geolocation.watchPosition(
          ok.bind(geolocation), err.bind(geolocation),
          geolocation.options
        );
      });
  },
  getPosition: function() { return this._promise('getPosition'); },
  watchPosition: function() {
    this.clearWatch();
    return this._promise('watchPosition');
  },
  clearWatch: function() {
    if (!this.watchID) return;
    navigator.geolocation.clearWatch(this.watchID);
    this.watchID = null;
  }
};

var geolocation = new Geolocation();
geolocation.watchPosition()
  .then(
    function(position) {
      console.log("latitude: " + position.coords.latitude + " - longitude: " + position.coords.longitude)
    },
    function(error) {
      console.log("error: " + error);
    }
  )

我尝试使用从 watchPosition/0 返回的中间 promise ,但它返回相同的结果。

我错过了什么?

最佳答案

回答我自己。

已关注 @Benjamin Gruenbaum关于使用回调的建议,可以将处理 geolocation.watchPosition 响应的单个回调与 Promise 结合起来,然后使用 then().catch() 模式(在notify函数中):

function Geolocation() {
  this._options = {
    enableHighAccuracy: true, 
    maximumAge        : 10000,
    timeout           : 7000
  }
};

Geolocation.prototype = {
  get watchID() { return this._watchID; },
  set watchID(watchID) { this._watchID = watchID; },
  get options() { return this._options; },
  // hasCapability: function() { return "geolocation" in navigator; },
  _promise: function(promise, cb) {
    var geolocation = this;
    return new Promise(function(ok, err) {
      if (promise == "getPosition")
        navigator.geolocation.getCurrentPosition(cb, cb,
          geolocation.options
        );
      else if (promise == "watchPosition")
        geolocation.watchID = navigator.geolocation.watchPosition(
          cb, cb, geolocation.options
        );
    });
  },
  getPosition: function(cb) { return this._promise("getPosition", cb); },
  watchPosition: function(cb) {
    this.clearWatch();
    return this._promise("watchPosition", cb);
  },
  clearWatch: function() {
    if (!this.watchID) return;
    navigator.geolocation.clearWatch(this.watchID);
    this.watchID = null;
  }
};

/* Testing functions from another module */
function log(Data) { console.log(Date() + " " + Data); };
function logOk({coords: {latitude: latitude, longitude: longitude}}) {
  log("latitude: " + latitude + " - longitude: " + longitude);
};
function logError({code: code, message: message}) {
  log("error geo " + code + " - " + message);
};

/* Callback from another module */
function notify(event) {
  return new Promise(
      function(ok, err) { event.coords ? ok(event) : err(event); }
    ).then(logOk).catch(logError);
};

/**/
var geolocation = new Geolocation();
// geolocation.getPosition(notify);
geolocation.watchPosition(notify);

不确定我是否正确使用 Promise,但它可以工作并且允许利用链接。

关于javascript - JS - Promise + geolocation.watchPosition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23942339/

相关文章:

javascript - 如何访问 javascript 类中新添加的函数

mysql - 如何在 MySQL 中查询具有地理空间 "point"字段的表

java - 使用 Twitter4j 收集带有地理标记的推文

algorithm - 地理哈希 : Using libgeohash to find neighbors

javascript - Jquery promise 链

javascript - promise : Resolve returns value too early

javascript - promise 错误消息端口在收到响应之前关闭

javascript - 使用 onclick 按钮重新加载 div

javascript - jQuery ReplaceWith 在 Firefox 42 中不起作用

javascript - 正常初始化变量和按对象初始化变量的区别