2014-10-24 2 views
2

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("&","&amp;",$mPost->post_title))."', ".$latlng[0].", ".$latlng[1].", ".$i."];"; 
      } else { 
      //Can't find lat/lng so get it from google 
       echo "codeAddress('".addslashes(str_replace("&","&amp;",$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] 
]; 

감사합니다.

+0

중복 가능성 [Ajax 호출의 응답을 반환하는 방법?] (http://stackoverflow.com/questions/14220321/how-to-return- a-aax 응답으로 응답) – user2864740

+0

왜냐하면'geocoder.geocode'는 값을 반환하지 않기 때문입니다. 관련 질문 : http://stackoverflow.com/q/14220321/218196, http://stackoverflow.com/q/23667086/218196 –

+0

수정 사항은 주어진 답변에 대해 변경되지 않습니다. –

답변

1

.geocode은 비동기 함수이므로 콜백을 사용해야합니다! 다음

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); 
     } 
    }); 
    console.log(thelocation); 
} 

그리고

는이 기능을 사용하려면이 complete..so 때 그냥 말할 수 없다 원인

codeAddress(business, address, i, locations, function(location) { 
    console.log(location); 
}); 
1

Theoritically, 당신은 aync 작업 외부 위치 값을 잡을 수 없어 말하기 당신의 거, 그래서 콜백 내부를 잡을 필요가

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 location; 
console.log(loc); 
//continue with what you would like to do with the results, something small i assume! :) 
    } 
    }); 

} 
+1

"이론적으로 말하면"? –

+0

좋은 답변, 당신이 약속을 말할 수있는 당신의 시점에 따라 이론적이 아니라 범위를 벗어납니다. 좋은 대답 – Pogrindis

+0

@ FelixKling 나는 그것을 즉석에서 잡을 수있는 방법이 있는지 궁금해. 물론 클라이언트 측 ... 재미있는 점 ... Observables? – Pogrindis

관련 문제