2011-09-08 2 views
1

マップ上に表示するためにGoogleマップのジオコードアドレスを使用しています。私は各アドレスをループし、jquery経由でコールバックを介して応答を処理しています。コールバックからオブジェクトを特定する

どのようにコールバックからのアイテムを特定できますか?

var geocoder = new google.maps.Geocoder(); 
geocoder.geocode({ 'address': addr }, function (results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     var marker = new google.maps.Marker({ position: results[0].geometry.location, map: map, title: "Yo Homie" }); 
     // I need to find the item to setup the infowindow 
     bounds.extend(results[0].geometry.location); 
     map.fitBounds(bounds); 
    } 
    else { 
     // eat it like a mexican jumping bean 
    } 
}); 

答えて

1

ジオコーダー呼び出しを関数にラップし、その関数にオブジェクトを渡してから、ジオコーダーのコールバックでも使用できます。

例:

// i'm just staging a fake for loop here... 
for(var i = 0; i < 10; i++){ 

    // this is your basic object 
    var obj = {address: "Brussels Belgium"}; 
    // here you call the geo function and pass the object so that you can access it inside the function 
    doGeoCode(obj); 
} 

function doGeoCode(obj) { 
    // your geocoder wrapped in a function, takes 1 argument which you can reference in the callback below 
    var geocoder; 
    geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'address': obj.address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     var p = results[0].geometry.location; 

     // inside this callback function you can just reference 'obj' 
     obj.position = {'longitude': p.lng(), 'latitude': p.lat()}; 
    } else { 
     // your mexican thingy 
    } 
    }); 
} 
0

もっとコードを入力してください。 あなたの現在のコードを見ると、あなたが投稿したコードはすべてループ内にあり、addrはあなたのリストの単一のアドレスだと仮定します。その場合は、コールバック関数(結果、ステータス)内でaddrにアクセスする必要があります。

関連する問題