0

역 Geocoading 작동하지 않습니다하지만 난 지금Google지도 : 반환 나는 그것의 작업을 ReverseGeCoding 노력하고

var address = reverseGeoCode(31.518945,74.349316); 

때마다 내 주소 변수처럼 내 함수를 호출 할 때 나는 반환 값

function reverseGeoCode(lat,lng) { 
var reverseGeoAddress = ''; 
var geocoder = new google.maps.Geocoder(); 
var latlng = new google.maps.LatLng(lat, lng); 
geocoder.geocode({'latLng': latlng}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     if (results[1]) { 
      if(results[1].formatted_address.length){ 
       reverseGeoAddress = results[1].formatted_address; 
       //NOTE: when i console.log(reverseGeoAddress); 
       //its working fine i am getting the address 
       return reverseGeoAddress; 
        //but return not working. 

      } 
     } 
     } 
    }); 
} 

을 얻을 질수 "정의되지 않음"; 왜 이렇게하고 있습니까 ?? 힌트가 있습니까? 이 비동기 함수이기 때문에 당신은 가능성이 콜백을 사용할 수 있습니다 -

답변

4

기능 reverseGeoCode

return reverseGeoAddress; is inside anonymous function. 

간단한 수정 될 어떤 리턴 값을 가지고 있지 않습니다. "콜백"은 호출 한 곳에서 처리기가 될 수 있습니다.

// Invoking reverseGeoCode.... 
reverseGeoCode(lat,lng, function(myAddress){ 
    // Your custom code goes here... 
}); 

function reverseGeoCode(lat,lng, callback) { 
var reverseGeoAddress = ''; 
var geocoder = new google.maps.Geocoder(); 
var latlng = new google.maps.LatLng(lat, lng); 
geocoder.geocode({'latLng': latlng}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     if (results[1]) { 
      if(results[1].formatted_address.length){ 
       reverseGeoAddress = results[1].formatted_address; 
       // Callback the handler if it exists here 
       // No return value 
       callback(reverseGeoAddress); 
      } 
     } 
     } 
    }); 
} 
관련 문제