Javascript 谷歌地图:函数返回未定义的值,在console.log中运行良好
我试图返回lat,lon作为它在console.log上打印数组的地址,但在函数return中不起作用Javascript 谷歌地图:函数返回未定义的值,在console.log中运行良好,javascript,google-maps,geolocation,Javascript,Google Maps,Geolocation,我试图返回lat,lon作为它在console.log上打印数组的地址,但在函数return中不起作用 var markers = new Array(); var cityCircles = new Array(); var polycordinates = new Array(); var previousepolygones = new Array(); var infoxboxs = new Array(); function initialize() { var mapProp
var markers = new Array();
var cityCircles = new Array();
var polycordinates = new Array();
var previousepolygones = new Array();
var infoxboxs = new Array();
function initialize() {
var mapProp = {
center:new google.maps.LatLng(44.03121020078675, 23.41763112193103),
zoom: <?= (isset($zoom) and $zoom != '') ? $zoom : '7' ?>,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
console.log(getcountrylatlng("Romania"));
}
function getcountrylatlng(countryname){
var geocoder = new google.maps.Geocoder();
geocoder.geocode(
{'address': countryname},
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
//var result = results[0].formatted_address;
//var city = result.split(" - ");
//console.log(results[0]);
return results[0];
}
}
}
);
}
var markers=new Array();
var cityCircles=新数组();
var polycordinates=新数组();
var previousepolygones=新数组();
var infoxboxs=新数组();
函数初始化(){
var mapProp={
中心:新google.maps.LatLng(44.03121020078675,23.4176311219103),
缩放:,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=new google.maps.map(document.getElementById(“googleMap”),mapProp);
console.log(getcountrylatlng(“罗马尼亚”));
}
函数getcountrylatlng(countryname){
var geocoder=new google.maps.geocoder();
地理编码(
{'address':countryname},
功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[0]){
//var result=results[0]。格式化的\u地址;
//var city=结果分割(“-”);
//console.log(结果[0]);
返回结果[0];
}
}
}
);
}
在console.log上,它返回一个对象,但如果我返回上述函数中的值,它将返回undefined
需要这方面的帮助时,您从错误的位置返回控制台日志的retrun位于内部函数中,因此不会返回给调用方函数
function getcountrylatlng(countryname){
var geocoder = new google.maps.Geocoder();
var myValue;
geocoder.geocode(
{'address': countryname},
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
//var result = results[0].formatted_address;
//var city = result.split(" - ");
//console.log(results[0]);
//return results[0];
myValue = results[0];
}
}
}
);
return myValue;
}
可能重复的