thelocation
이이 예제에서 정의되지 않은 상태로 반환되는 이유를 알려 줄 수 있습니까?자바 스크립트 함수가 다른 함수 내부에서 값을 반환하지 않음
function codeAddress(business,address,i,locations) {
var thelocation = geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var loc = [business, results[0].geometry.location.lat(), results[0].geometry.location.lng(), i];
return loc;
}
});
console.log(thelocation);
}
많은 도움에 감사드립니다.
편집
여기에 내가 어떤 값이 & LNG하지만 다른 사람 만 주소 위도로 데이터베이스에서 나오는이 ... 전체 맥락이다. 마커가있는지도를 만들기 위해 배열을 채워야하지만 일부는 지오 코더를 통과해야합니다.
var locations = [];
geocoder = new google.maps.Geocoder();
<?php
if (!empty($posts)) {
$i = 1;
foreach ($posts as $mPost) {
//If we find a lat/lng override value
if (get_field('lat_lng_override', $mPost->ID)) {
$latlng = explode(",",get_field('lat_lng_override', $mPost->ID));
echo "locations[{$i}] = ['". addslashes(str_replace("&","&",$mPost->post_title))."', ".$latlng[0].", ".$latlng[1].", ".$i."];";
} else {
//Can't find lat/lng so get it from google
echo "codeAddress('".addslashes(str_replace("&","&",$mPost->post_title))."','".get_field('business_address', $mPost->ID).", ".get_field('city', $mPost->ID).", ON',".$i.",locations, function(loc){ locations.push(loc); });";
}
$i++;
}
}
?>
console.log(locations);
function codeAddress(business,address,i,locations, callback) {
var thelocation = geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var loc = [business, results[0].geometry.location.lat(), results[0].geometry.location.lng(), i];
callback(loc);
}
});
}
아이디어는 내가 다있을 때 내 배열이 비슷합니다 것입니다 : 여기에 내가 분명히 작동하지 않습니다이 무엇을 사전에
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
감사합니다.
중복 가능성 [Ajax 호출의 응답을 반환하는 방법?] (http://stackoverflow.com/questions/14220321/how-to-return- a-aax 응답으로 응답) – user2864740
왜냐하면'geocoder.geocode'는 값을 반환하지 않기 때문입니다. 관련 질문 : http://stackoverflow.com/q/14220321/218196, http://stackoverflow.com/q/23667086/218196 –
수정 사항은 주어진 답변에 대해 변경되지 않습니다. –