자바 스크립트 문제가 있습니다. 검색했지만 문제와 관련성이없는 것은 없습니다.자바 스크립트에서 값을 반환하는 중
기본적으로 나는 두 개의 다른 함수 (codeAddressLat 및 codeAddressLng)를 호출하는 함수 (addToGlobe)가 있습니다. 두 개의 호출 된 함수는 모두 첫 번째 함수에 float 값을 반환해야하며, 두 번째 함수는이를 사용합니다. 하위 함수는 정확하게 올바르게 작동합니다. print 문을 사용하여 각 변수의 "numfinal"변수에 값이 있는지 확인합니다.
그러나 코드에 주석으로 표시된 호출 함수에 print 문을 추가하면 'undefined'가 반환됩니다. 따라서 numfinal 값이 반환 될 때 문제가있는 것 같습니다.
감사합니다 :) 당신은 codeAddressLat
에 return 문이없는
function addToGlobe(uname, uid, pmcity) {
// Get lat & long of city
var pmlat = codeAddressLat(pmcity);
var pmlong = codeAddressLng(pmcity);
log(pmlat); // PROBLEM! Prints 'undefined'
log(pmlong); // PROBLEM! Prints 'undefined'
// Rest of function removed to keep it simple
}
function codeAddressLat(inputcity) {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
geocoder.geocode({ 'address': inputcity}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var llsplit = new Array();
bkresult = String(results[0].geometry.location);
bkresult = bkresult.replace(/[\(\)]/g, "");
llsplit = bkresult.split(',');
numfinal = parseFloat(llsplit[0]);
return numfinal;
} else {
log('<b><font color="#C40031">Geocode was not successful:</b> ' + status);
}
});
}
function codeAddressLng(inputcity) {
// Basically the same function as above. Removed for simplicity
}
위대한 작품, 고마워요! :) –