按照请求的相同顺序存储异步方法的响应
store responses of an async method in the same order they are asked
我需要在数组中存储一些 google 地图坐标,我通过地理编码器获取坐标,因此我传递城市名称或地址,它 returns 给我坐标。
我发现地理编码器一定是一个异步函数,因为它没有按照我问他们的顺序给我答案。所以坐标以我需要的不同顺序存储。
例如,如果我要求 barcelona -> paris -> barcelona,答案始终是 barcelona -> barcelona -> paris 或 paris -> barcelona -> barcelona 的坐标。或者如果我问 paris -> roma -> oslo 它会以另一个顺序回答。
var address = ['Barcelona', 'Viena','Oslo'];
for (var i = 0; i < address.length; i++ ) {
geocoder.geocode({
'address': address[i]
}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
lineCoordinates.push(results[0].geometry.location);
cities.push(results[0].address_components[0].long_name);
console.log("he terminado para:" + results[0].address_components[0].long_name);
}
});
}
我怎样才能以正确的顺序存储响应?
谢谢
您可以根据自己的目的使用回调。将您的地理编码请求发送到如下函数:
doGeocode: function (address, postal_code, callback) {
console.log("TEST: " + address.toString());
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address,
'componentRestrictions': {
'postalCode': postal_code,
'country': 'de'
}
}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
console.log(results);
callback(results);
} else {
//Error handling
alert('Geocode was not successful for the following reason: ' + status);
}
});
现在您可以在每个回调中工作,例如:
doGeocode (adress, postal_code, function (response1){
//Barcelona
doGeocode (adress, postal_code, function (response2){
//Viena
doGeocode (adress, postal_code, function (response3){
//Oslo
//here you have all your requested information from all 3 requests
)};
)};
)};
地址、线坐标和城市中的每一个都有数组。我会放弃它,只有 1 个包含所有内容的数组,因此您可以将您正在请求的城市与其坐标和名称联系起来。
var cities = [
{
name: 'Barcelona',
longName: '',
coordinates: ''
},
{
name: 'Viena',
longName: '',
coordinates: ''
},
{
name: 'Oslo',
longName: '',
coordinates: ''
}
];
for (var i = 0; i < cities.length; i++ ) {
geocoder.geocode({
'address': cities[i].name
}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
cities[i].coordinates = results[0].geometry.location;
cities[i].longName = results[0].address_components[0].long_name;
}
});
}
我需要在数组中存储一些 google 地图坐标,我通过地理编码器获取坐标,因此我传递城市名称或地址,它 returns 给我坐标。
我发现地理编码器一定是一个异步函数,因为它没有按照我问他们的顺序给我答案。所以坐标以我需要的不同顺序存储。
例如,如果我要求 barcelona -> paris -> barcelona,答案始终是 barcelona -> barcelona -> paris 或 paris -> barcelona -> barcelona 的坐标。或者如果我问 paris -> roma -> oslo 它会以另一个顺序回答。
var address = ['Barcelona', 'Viena','Oslo'];
for (var i = 0; i < address.length; i++ ) {
geocoder.geocode({
'address': address[i]
}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
lineCoordinates.push(results[0].geometry.location);
cities.push(results[0].address_components[0].long_name);
console.log("he terminado para:" + results[0].address_components[0].long_name);
}
});
}
我怎样才能以正确的顺序存储响应?
谢谢
您可以根据自己的目的使用回调。将您的地理编码请求发送到如下函数:
doGeocode: function (address, postal_code, callback) {
console.log("TEST: " + address.toString());
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
'address': address,
'componentRestrictions': {
'postalCode': postal_code,
'country': 'de'
}
}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
console.log(results);
callback(results);
} else {
//Error handling
alert('Geocode was not successful for the following reason: ' + status);
}
});
现在您可以在每个回调中工作,例如:
doGeocode (adress, postal_code, function (response1){
//Barcelona
doGeocode (adress, postal_code, function (response2){
//Viena
doGeocode (adress, postal_code, function (response3){
//Oslo
//here you have all your requested information from all 3 requests
)};
)};
)};
地址、线坐标和城市中的每一个都有数组。我会放弃它,只有 1 个包含所有内容的数组,因此您可以将您正在请求的城市与其坐标和名称联系起来。
var cities = [
{
name: 'Barcelona',
longName: '',
coordinates: ''
},
{
name: 'Viena',
longName: '',
coordinates: ''
},
{
name: 'Oslo',
longName: '',
coordinates: ''
}
];
for (var i = 0; i < cities.length; i++ ) {
geocoder.geocode({
'address': cities[i].name
}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
cities[i].coordinates = results[0].geometry.location;
cities[i].longName = results[0].address_components[0].long_name;
}
});
}