如何设置地理编码调用的结果值?
How to set result value from geocode call?
您好,我正在尝试将用户当前位置存储到 MONGODB
,但我无法将结果值设置为 global_variable?
尝试下面的代码
变量global_variable;
getCurrentAddress(location) {
this.currgeocoder.geocode({
'location': location
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log("Results:::" + JSON.stringify(results[0]));
**global_variable = results;**
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
但是global_variable是undefined
请问有人帮帮我吗?提前致谢!!
这是一个基于承诺的地址调用示例,这样您就可以从 API 调用中获取数据,以便能够传递给 MongoDB。当然,另一种选择是在 geocode
函数本身的回调中进行 MongoDB 调用:
getCurrentAddress = function(location, currgeocoder) {
return new Promise(function(resolve, reject) {
currgeocoder.geocode({
'location': location
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log("Results:::" + JSON.stringify(results[0]));
resolve(results)
} else {
reject('Geocode was not successful for the following reason: '
+ status);
}
})
});
};
getCurrentAddress(/*some location data*/, this.currgeocoder)
.then(function(results) { console.log(results); //pass to MongoDB here})
.catch(function(error) { console.error(error); //handle error})
您好,我正在尝试将用户当前位置存储到 MONGODB
,但我无法将结果值设置为 global_variable?
尝试下面的代码
变量global_variable;
getCurrentAddress(location) {
this.currgeocoder.geocode({
'location': location
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log("Results:::" + JSON.stringify(results[0]));
**global_variable = results;**
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
但是global_variable是undefined
请问有人帮帮我吗?提前致谢!!
这是一个基于承诺的地址调用示例,这样您就可以从 API 调用中获取数据,以便能够传递给 MongoDB。当然,另一种选择是在 geocode
函数本身的回调中进行 MongoDB 调用:
getCurrentAddress = function(location, currgeocoder) {
return new Promise(function(resolve, reject) {
currgeocoder.geocode({
'location': location
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log("Results:::" + JSON.stringify(results[0]));
resolve(results)
} else {
reject('Geocode was not successful for the following reason: '
+ status);
}
})
});
};
getCurrentAddress(/*some location data*/, this.currgeocoder)
.then(function(results) { console.log(results); //pass to MongoDB here})
.catch(function(error) { console.error(error); //handle error})