2012-04-22 6 views
0

Google Maps API에서 작업 중입니다. 나는 왜 아래 함수가 인덱스 ++ 이후에 호출되는지 알지 못한다. 내가 아는 한 ReverseGeocode()가 먼저 호출되어야합니다. 그 대신에 그것은 나를 위해 문제를 만들고있는 함수를 먼저 증가시키고 호출하는 것입니다. 경고 상자는 작성된대로 표시되지만 중간 기능은 함수의 마지막 줄이 실행 된 후 (즉, index ++) 호출됩니다.실행중인 함수 호출 후 Javascript 함수가 완료됩니다.

 function placeMarker(location) 
     { 
      alert("iiii"); 
      ReverseGeocode(location.lat(),location.lng()); 
      alert("jjjk"); 
      index++; 
     } 

은 여기 내 ReverseGeoCode

function ReverseGeocode(lat,lng) {  
    var latlng = new google.maps.LatLng(lat, lng); 
    geocoder.geocode({'latLng': latlng}, function(results, status) 
    { 
     if (status == google.maps.GeocoderStatus.OK) 
    { 
      if (results[1]) 
     { 

      places[index]=results[0].formatted_address; 
      alert(places[index]+"index="+index); 
      AddRow('table',results[0].formatted_address); 
      document.getElementById("dataa").innerHTML+=results[0].formatted_address+"<br/>"; 
     } 
    } 
    else 
    { 
    alert("Geocoder failed due to: " + status); 
     } 
    }); 
    } 

설명해주십시오이다. 미리 감사드립니다.

+1

** ReverseGeocode ** 기능을 게시 할 수 있습니까? – McGarnagle

+0

'ReverseGeoCode'가'index ++'다음에 실행되는 것을 어떻게 결정합니까? – Deestan

+0

@dbaseman 질문을 편집했습니다. – Mj1992

답변

1

경고는 geocoder.geocode이 계산을 완료 할 때 실행되는 콜백 함수 내부에 있습니다.

geocoder.geocode은 비동기로 나타납니다. 일반적으로 이것은 geocoder.geocode이 다른 곳에서 작업을 시작하면서 프로그램이 지역 결론을 내리는 것을 의미합니다. geocoder.geocode 나중에 완료되면 제공된 콜백 함수가 실행됩니다.

+0

hmmm thnx got it – Mj1992

0

나는 geocoder.geocode이 비동기라고 생각합니다. 나중에 index의 값이 증가 할 때 익명 함수를 실행하고 있습니다. 덮어 쓰기되지 않도록

function placeMarker(location) 
{ 
alert("iiii") 
ReverseGeocode(location.lat(),location.lng(),index); 
alert("jjjk"); 
index++; 

}이 경우

function ReverseGeocode(lat,lng,index) {     
     var latlng = new google.maps.LatLng(lat, lng); 
     geocoder.geocode({'latLng': latlng}, function(results, status) 
  { 
      if (status == google.maps.GeocoderStatus.OK) 
     { 
           if (results[1]) 
      { 

          places[index]=results[0].formatted_address; 
          alert(places[index]+"index="+index); 
          AddRow('table',results[0].formatted_address); 
          document.getElementById("dataa").innerHTML+=results[0].formatted_address+"<br/>"; 
      } 
  } 
  else 
  { 
    alert("Geocoder failed due to: " + status); 
      } 
    }); 
  } 

, index은 익명 함수의 로컬 범위로 간다.