1

我希望以参数的形式function getCoordinates将返回的对象文字(经度和纬度坐标)传递给function getWeather,这样我就可以function getCoordinatesfunction getWeather. 或者换句话说,我希望将一个函数的输出作为输入传递给另一个函数,以便能够访问它的数据。我将如何实现这一目标?

到目前为止,这是我的 JavaScript 代码:

var APP = {
    getCoordinates: function () {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                var coordinates = {
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude
                }
                return coordinates;
            });
        } else {
            console.log("Geolocation isn't supported in your browser.");
        }
    },

    getWeather: function (getCoordinates) {
        var api_key = '1234567890',
            weather_query = 'http://api.wunderground.com/api/';
            weather_query += api_key + '/geolookup/q/';
            weather_query += longitude + ',' + latitude + '.json';
    }
}
4

6 回答 6

2
var APP = {
    getCoordinates: function () {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(this.getWeather);
        } else {
            console.log("Geolocation isn't supported in your browser.");
        }
    },
    getWeather: function (position) {
        var latitude       = position.coords.latitude,
            longitude      = position.coords.longitude,
            api_key        = '1234567890',
            weather_query  = 'http://api.wunderground.com/api/';
            weather_query += api_key + '/geolookup/q/';
            weather_query += longitude + ',' + latitude + '.json';

        // do weather lookup here, inside success handler of
        // the geolocation call
    }
}

APP.getCoordinates();

小提琴

于 2013-07-14T05:41:14.897 回答
1

只是returning 坐标不会做任何事情。你必须接受一个回调参数:

var APP = {
    // Get the coordinates, and then do something with them
    getCoordinates: function (callback) {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                var coordinates = {
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude
                }
                // Coordinates are ready, pass to the processing function
                callback(coordinates);
            });
        } else {
            console.log("Geolocation isn't supported in your browser.");
        }
    },

    getWeather: function (coordinates) {
        var api_key = '1234567890',
            weather_query = 'http://api.wunderground.com/api/';
            weather_query += api_key + '/geolookup/q/';
            weather_query += coordinates.longitude + ',' + coordinates.latitude + '.json';
    }
}

APP.getCoordinates(APP.getWeather);
于 2013-07-14T05:38:11.897 回答
1

你试图做的事情是不可能的。所以你需要重新考虑。请注意,它navigator.geolocation.getCurrentPosition()需要一个回调函数。正如@JonathanLonowski 指出的那样,这意味着该函数是asynchronous。也就是说,只要传递给它的值可用,就会及时调用回调函数。

从这个回调返回一个值没有任何好处。相反,您需要做的是在该回调中或在回调调用的另一个函数中执行任何必需的操作(本质上是相同的事情)。

什么代码调用getWeather()?这就是您需要查看的代码。不应只是在任意时间进行调用,而应从getCurrentPosition()回调中进行调用,并且无论您做什么都weather_query应该在当时和那里完成。

由于代码现在就坐,getWeather()实际上并没有任何事情。它创建了几个局部变量,然后被丢弃。所以这可能是下一个要看的问题。

无论你做什么,底线都是一样的:任何依赖于回调的动作都coordinates需要getCurrentPosition()在回调被调用时发生。

正如@adeneo 所提到的,您可以使用诸如jQuery 的promise 之类的工具来帮助编写更简洁的代码来执行此操作。但是没有任何工具可以改变根本问题:您必须等到数据准备好,并且这种或另一种方式相当于直接或间接地从该回调中采取行动。

于 2013-07-14T05:41:45.213 回答
1

Adeneo 有一个很好的答案。只是为了补充它:

让我们进行更改getCoordinates,以便它现在需要一个回调,该回调可以对位置数据进行任何您想要的处理。

getCoordinates: function (callback) {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(callback);
        } else {
            console.log("Geolocation isn't supported in your browser.");
        }
    },

现在,getWeather 所要做的就是提供回调函数。

getWeather: function () {
        var callback = function(position){
            var coordinates = {
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude
             };
            var api_key = '1234567890',
            weather_query = 'http://api.wunderground.com/api/';
            weather_query += api_key + '/geolookup/q/';
            weather_query += coordinates.longitude + ',' + coordinates.latitude + '.json';
            console.log(weather_query);
        };
        APP.getCoordinates(callback);

    }

您可以继续使用APP.getWeather来获取您的数据。此模式可以扩展为您需要对位置数据进行的任何其他类型的处理。

于 2013-07-14T05:53:03.547 回答
0

为什么不喜欢这样?见功能变化getWeather()

var APP = {
    getCoordinates: function () {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function (position) {
                var coordinates = {
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude
                }
                return coordinates;
            });
        } else {
            console.log("Geolocation isn't supported in your browser.");
        }
    },

    getWeather: function (coordinates) {
        var api_key = '1234567890',
            weather_query = 'http://api.wunderground.com/api/';
            weather_query += api_key + '/geolookup/q/';
            weather_query += coordinates.longitude + ',' + coordinates.latitude + '.json';
    }
}

// function call:

APP.getWeather(APP.getCoordinates());
于 2013-07-14T05:30:40.163 回答
0

您可以使用回调函数来执行此操作,例如

var APP = {
    getCoordinates: function (cb) {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(cb);
        } else {
            console.log("Geolocation isn't supported in your browser.");
        }
    },

    getWeather: function () {
        this.getCoordinates(function(cord){
            var api_key = '1234567890',
            weather_query = 'http://api.wunderground.com/api/';
            weather_query += api_key + '/geolookup/q/';
            weather_query += cord.longitude + ',' + cord.latitude + '.json';

            // now you have it
            console.log(weather_query);
        });
    }
}
APP.getWeather();

演示。

于 2013-07-14T05:31:08.183 回答