Javascript从异步函数返回值 我知道这是一个非常受欢迎的话题,很多时候都能回答,但我 无法完全理解它在我的案例中的应用。
我正在通过Javascript学习我的方法,我一直在尝试从异步函数返回一个值。本质上,我希望我的函数采用城市名称,并从APIXU Weather API()返回其当前温度 然后,我想在我的另一个GoogleAPI函数中使用这个函数。此函数获取所单击标记的id,在地点数组中查找其城市名称,并在信息窗口中显示信息,如地点名称、城市天气和Google Streetview。我最初的计划是在PopulateInfo窗口中使用getWeather函数来查找城市的天气。有可能实现吗?因为据我所知,在synchronous中使用异步函数,将使最新的一个异步。我要选择的另一种可能的方法是在加载文档时获取数组中所有位置的天气,然后在populateInfo窗口函数中从数组中获取信息。不过,我恐怕这会是重复和不必要的 在您看来,实现这一目标的最佳方式是什么?如何避免类似问题 代码如下:Javascript从异步函数返回值 我知道这是一个非常受欢迎的话题,很多时候都能回答,但我 无法完全理解它在我的案例中的应用。,javascript,json,asynchronous,google-maps-api-3,weather-api,Javascript,Json,Asynchronous,Google Maps Api 3,Weather Api,我正在通过Javascript学习我的方法,我一直在尝试从异步函数返回一个值。本质上,我希望我的函数采用城市名称,并从APIXU Weather API()返回其当前温度 然后,我想在我的另一个GoogleAPI函数中使用这个函数。此函数获取所单击标记的id,在地点数组中查找其城市名称,并在信息窗口中显示信息,如地点名称、城市天气和Google Streetview。我最初的计划是在PopulateInfo窗口中使用getWeather函数来查找城市的天气。有可能实现吗?因为据我所知,在sync
//Get current forecast from weather API
function getWeather(city) {
var weatherAPIXU = "http://api.apixu.com/v1/current.json?key=XXXXXXXXXXXXXXX&q=" + city;
$.getJSON(weatherAPIXU, function(data) {
var forecast = data.current.temp_c;
var curWeather = forecast + '° C';
return curWeather
});
}
// Return a city name that matches a marker id
function getCityName(locations, marker) {
for (var i = 0, iLen = locations.length; i < iLen; i++) {
if (locations[i].id == marker.id) return locations[i].city;
}
}
// This function populates the infowindow when the marker is clicked. It'll only allow
// one infowindow which will open at the marker that is clicked, and populate based
// on that markers position.
function populateInfoWindow(marker, infowindow) {
// Check to make sure the infowindow is not already opened on this marker.
if (infowindow.marker != marker) {
// Clear the infowindow content to give the streetview time to load.
infowindow.setContent('');
infowindow.marker = marker;
// Make sure the marker property is cleared if the infowindow is closed.
infowindow.addListener('closeclick', function() {
infowindow.marker = null;
});
var streetViewService = new google.maps.StreetViewService();
var radius = 50;
var city = getCityName(locations, marker);
console.log(city);
var weatherInCity = getWeather(city);
console.log(weatherInCity);
// In case the status is OK, which means the pano was found, compute the
// position of the streetview image, then calculate the heading, then get a
// panorama from that and set the options
var getStreetView = function(data, status) {
if (status == google.maps.StreetViewStatus.OK) {
var nearStreetViewLocation = data.location.latLng;
var heading = google.maps.geometry.spherical.computeHeading(
nearStreetViewLocation, marker.position);
infowindow.setContent('<div>' + marker.title + '' + weatherInCity +
'</div><div id="pano"></div>');
var panoramaOptions = {
position: nearStreetViewLocation,
pov: {
heading: heading,
pitch: 30
}
};
var panorama = new google.maps.StreetViewPanorama(
document.getElementById('pano'),
panoramaOptions);
} else {
infowindow.setContent('<div>' + marker.title +
'</div>' +
'<div>No Street View Found</div>');
}
};
// Use streetview service to get the closest streetview image within
// 50 meters of the markers position
streetViewService.getPanoramaByLocation(marker.position,
radius, getStreetView);
// Open the infowindow on the correct marker.
infowindow.open(map, marker);
}
}
//从天气API获取当前天气预报
天气(城市)功能{
var Weatherapix=”http://api.apixu.com/v1/current.json?key=XXXXXXXXXXXXXXX&q=“+城市;
$.getJSON(weatherAPIXU,函数(数据){
var预测=data.current.temp_c;
var curWeather=天气预报+摄氏度;
回程天气
});
}
//返回与标记id匹配的城市名称
函数getCityName(位置、标记){
对于(变量i=0,iLen=locations.length;i
您的函数可能会返回一个值,我猜您正在尝试在函数返回之前使用该值 不能像这样返回对象,因为它是回调函数。您可以在回调函数中编写逻辑
function getWeather() {
var city = $("#txtCity").val();
var weatherAPIXU = "http://api.apixu.com/v1/current.json?key=XXXXXXX&q=" + city;
$.getJSON(weatherAPIXU, function(data) {
var forecast = data.current.temp_c;
var curWeather = forecast + '° C';
$("#lblTemp").html(curWeather)
});
另一个不推荐的选项是声明全局变量并将返回值分配给全局变量
var CurrentWeather = null
function getWeather(city) {
var weatherAPIXU = "http://api.apixu.com/v1/current.json?key=XXXXX&q=" + city;
$.getJSON(weatherAPIXU, function(data) {
var forecast = data.current.temp_c;
var curWeather = forecast + '° C';
$("#lblTemp").html(curWeather)
CurrentWeather = curWeather;
});
我真的很希望大学能认真开始教授异步编程。不管是JavaScript还是C++(是的,都有C++异步框架:签出GTK)或java或TCL。我厌倦了这个问题Grechko,不要被slebetman的评论所冒犯。“我厌倦了这个问题”这句话不是针对你个人的!只是这个问题的变体被很多人问了很多次。即使是最乐于助人的人有时也会说“哦,不,别再提这个问题了!”:-)我看了一下链接的“重复”问题的答案,几乎所有的答案都很糟糕。他们对角度、承诺和所有那些会让你头晕目眩的东西进行了如此详细的讨论!您需要学习的基本点非常简单:从
$.getJSON()
回调返回任何值,并认为您的其他代码可以使用该值是没有用的。异步回调不是这样工作的。相反,您需要做的只是在回调内部或从回调调用的另一个函数中处理JSON数据