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에 대한 액세스 권한이 있어야합니다.