2012-01-17 19 views
1

ある関数から別の関数に2つの座標を渡す際に問題が発生しています。私は実際にJavaScriptを知らないが、いくらか正しいと思われる。私の誤りがどこにあるのか教えてください。ある関数から別の関数に変数を渡すJavaScript

 <script type="text/javascript"> 

     function initialize() { 
      var address = "Chicago IL"; 
      locs= getLatLong(address); 

      alert(locs.lat()); //does not work 
      var myOptions = { 
       zoom: 4, 
       center: new google.maps.LatLng(41, -87), 
       mapTypeId: google.maps.MapTypeId.ROADMAP   
      }; 

      var map = new google.maps.Map(document.getElementById('map_canvas'), 
      myOptions); 
     } 

     function getLatLong(address){ 
      var geo = new google.maps.Geocoder; 
      geo.geocode({'address':address},function(results, status){ 
       if (status == google.maps.GeocoderStatus.OK) { 

        locations[0] = results[0].geometry.location.lat(); 
        locations[1] = results[0].geometry.location.lng();     

        //alert(locations[0]); //test is good- works! Need to pass it back to function 
        //alert(locations[1]); //test is good- works! Need to pass it back to function 
        locs =results[0].geometry.location; 
        return locs;     
       } else { 
        alert("Geocode was not successful for the following reason: " + status); 
       } 
      }); 
     } 
    </script> 
+0

私は、基本的な(まだ難しい)理解するのを助けるかもしれない同様の質問をしました:http://stackoverflow.com/questions/8436358/understanding-local-scope – ajax333221

答えて

2

非同期関数から値を返すことはできません。試してはいけない。

代わりにコールバックを使用してください。

function setLocationOnMap(locs) { 
    alert(locs.lat()); // works now! 
    var myOptions = { 
     zoom: 4, 
     center: new google.maps.LatLng(41, -87), 
     mapTypeId: google.maps.MapTypeId.ROADMAP   
    }; 
    var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions); 
} 

function initialize() { 
    var address = "Chicago IL"; 
    getLatLong(address, setLocationOnMap); 
} 

function getLatLong(address, callback){ 
    var geo = new google.maps.Geocoder; 
    geo.geocode({'address':address},function(results, status){ 
     if (status == google.maps.GeocoderStatus.OK) { 
      // processing... 
      locs = results[0].geometry.location; 

      callback(locs); 
     } else { 
      alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
} 

あなたはどんな結果が準備ができている、すなわち前に非同期関数は、すぐにgeo.geocode()リターンのように呼び出すことを理解する必要があります。だから、returnを使ってそれらの値を返すことができないのです。彼らにはまだ値がありません。

結果(ほとんどの場合、HTTP要求)は準備ができたら、非同期関数はコールバック関数を使用してそれを処理します。そのコールバック関数では、直接処理するか別の関数呼び出しを行うことで、さらに処理する必要があります。

+0

皆さん、私はかなり混乱しています。 この地図にマーカーを追加する必要があります。それはforループに入り、他の都市の追加の場所を確認する必要がありました。どのように私はコールバックでそれらを得ることができますか?申し訳ありませんが、オブジェクトと変数の受け渡しを理解していますが、以前はカルバックのコンセプトに遭遇していませんでした。 \t $ var position = new google.maps.LatLng(locs.lat()、locs.lng()); varマーカー=新しいgoogle.maps.Marker({ 位置:位置、 地図:地図 }); marker.setTitle( "Chicago、IL"); – Andrew

+1

N個のマーカーを設定するには、 'geocode()'へのN個の要求が必要です。 'geocode()'関数はすでにコールバックを受け付けます(引数として渡す 'function()に注意してください)。このコールバックを使用してマーカーを設定するだけです。 – Tomalak

関連する問題