2012-04-22 14 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は、匿名関数のローカルスコープに入ります。

関連する問題