Javascript 使地理位置坐标可用于其他功能

Javascript 使地理位置坐标可用于其他功能,javascript,geolocation,Javascript,Geolocation,我正在开发一个基本的JavaScript天气应用程序。我想从浏览器中提取地理位置,并使其可供我的其他功能访问 function Weather( options ) { this.apiKey = options.key; this.endpoint = options.endpoint; this.coordinates = {}; } Weather.prototype.GetLocation = function( callback ) { if (na

我正在开发一个基本的JavaScript天气应用程序。我想从浏览器中提取地理位置,并使其可供我的其他功能访问

function Weather( options ) {

    this.apiKey = options.key;
    this.endpoint = options.endpoint;
    this.coordinates = {};
}

Weather.prototype.GetLocation = function( callback ) {
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition( function(position) {
            this.coordinates.latitude = position.coords.latitude;
            this.coordinates.longitude = position.coords.longitude;
        });
    }
};

Weather.prototype.GetCurrentConditions = function() {

    this.GetLocation();

    var url = this.endpoint + "weather?lat=" + this.coordinates.latitude + "&lon=" + this.coordinates.longitude;

    return this.getJSONP( url );
};

Weather.prototype.GetExtendedForecast = function() {

    this.GetLocation();

    var url = this.endpoint + "forecast/daily?lat=" + this.coordinates.latitude + "&lon=" + this.coordinates.longitude + "&cnt=7";

    return this.getJSONP( url );
};

然而,我的纬度和经度一直没有定义。我在某个地方读到,如果你想使用坐标,你必须将它们保存在getCurrentPosition的回调函数中,我就是这么做的,但是除了未定义之外,我仍然无法得到任何东西。有没有办法让我的其他函数可以访问这些值?

首先,
navigator.geolocation.getCurrentPosition
回调异步工作,因此调用
this.GetLocation()
后不会立即得到结果。此外,用户可以禁止与应用程序共享其位置

其次,您的
错误。您需要在此处将上下文传递给回调:

navigator.geolocation.getCurrentPosition( function(position) {
    // "this" doesn't equal to instance of your Weather class
    this.coordinates.latitude = position.coords.latitude;
    this.coordinates.longitude = position.coords.longitude;
});
例如,您可以使用
.bind(this)
将预期的上下文传递给回调:

navigator.geolocation.getCurrentPosition( function(position) {
    this.coordinates.latitude = position.coords.latitude;
    this.coordinates.longitude = position.coords.longitude;
}.bind(this));
我的解决方案

最好用于异步代码。你可以使用任何承诺的实现,我更喜欢

我的代码是:

var getCurrentPosition = function() {
  var deferred = $.Deferred();

  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(deferred.resolve, deferred.reject);
  } else {
    deferred.reject({
      error: 'browser doesn\'t support geolocation'
    });
  }

  return deferred.promise();
};
如何使用它

var userPositionPromise = getCurrentPosition();

userPositionPromise
  .then(function(data) {
    // do whatever you want with geolocation data
  })
  .fail(function(error) {
    // show error for user
  });

您可以随时使用
userPositionPromise
,它将作为用户位置数据的容器,您无需再次调用
getUserPosition
。要访问数据,您需要为
userPositionPromise
变量调用
。然后(回调)

如果没有jQuery,您将如何进行绑定?我正试图在我的项目中更好地使用纯JavaScript。您可以在不使用原生jQuery的情况下编写它,它看起来是一样的,但是它(因此,您需要使用)。或者你可以不做任何承诺就写这篇文章,但是你需要使用大量回调和标志来检查数据。在你的JSFIDLE中,我看到你有一个拒绝,但是你在哪里解析坐标呢?我调用
navigator.geolocation.getCurrentPosition
并传递两个回调:
resolve
reject
。当
navigator.geolocation.getCurrentPosition
成功执行时,它调用
resolve
callback,用数据解析我的承诺。因此,我只是将promise中的
resolve
作为onSuccess回调传递给
getCurrentPosition
。你可以这样写:但是第一个版本看起来更干净。然而,我得到一个错误,说失败不是一个函数。你能告诉我我做错了什么吗?