2017-12-01 10 views
-1

Google지도에서 사용할 수 있도록 지오 코딩 된 약 60 개의 주소를 반복합니다. 내 콜백 (아래)은 위치를 수집하는 데는 효과가있는 것처럼 보이지만 루프를 통과하는 주소 개체에 연결하는 방법을 알아야합니다. 지오 코드 응답에서 내 요청이 무엇인지 알 수있는 것을 찾을 수 없습니다.내 지오 코더 지오 코드 콜백에서 결과가 어떤 요청에 해당하는지 결정하려면 어떻게해야합니까?

할 방법이 있습니까? 자바 스크립트에서

geocode() { 
    var lv_location; 
    var geocoder = new google.maps.Geocoder(); 

    if (geocoder) { 
     geocoder.geocode({'address' : this.address}, 
      function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        // what data in results can be used to relate the location to the address? 
        lv_location = results[0].geometry.location; 
       } 
       markerCounter++; 
       if (markerCounter >= 60) finishSurnames(); 
     }); 
    } 

답변

0

당신은 또한 폐쇄로 알려진 기능의 범위를 만들 것 Immediately-invoked function expression를 사용할 수 있습니다

내 기능입니다. 당신은

geocode() { 
    var lv_location; 
    var geocoder = new google.maps.Geocoder(); 

    if (geocoder) { 
     geocoder.geocode({'address' : this.address}, (function(originalAddress){ 
      return function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        // what data in results can be used to relate the location to the address? 
        //You can use the originalAddress variable here to relate result to request 
        lv_location = results[0].geometry.location; 

       } 
       markerCounter++; 
       if (markerCounter >= 60) finishSurnames(); 
      }; 
     })(this.address)); 
    } 
} 

내 예제를 보라에 비슷한으로 기능을 변경해야합니다, 그것은 3 개 주소와 콘솔 결과에 인쇄 및 해당 요청 문자열

var addresses = [ 
 
    'av Diagonal 197, Barcelona', 
 
    'av Lucas Vega 53, San Cristobal de La Laguna', 
 
    'Metrologichna 14, Kiev' 
 
]; 
 

 
function initMap() { 
 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
    zoom: 8, 
 
    center: {lat: -34.397, lng: 150.644} 
 
    }); 
 
    var geocoder = new google.maps.Geocoder(); 
 

 
    addresses.forEach(function(address) { 
 
    geocode(geocoder, address); 
 
    }); 
 
} 
 

 
function geocode(geocoder, address) { 
 
    geocoder.geocode({'address': address}, (function(originalAddress) { 
 
    return function(results, status) { 
 
     if (status === 'OK') { 
 
     console.log("Search: " + originalAddress + "->" + results[0].geometry.location.toString()); 
 
     } else { 
 
     console.log("Search: " + originalAddress + "->" + status); 
 
     } 
 
    }; 
 
    })(address)); 
 
}
#map { 
 
    height: 100%; 
 
} 
 
html, body { 
 
    height: 100%; 
 
    margin: 0; 
 
    padding: 0; 
 
}
<div id="map"></div> 
 
<script async defer 
 
    src="https://maps.googleapis.com/maps/api/js?v=3&key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&callback=initMap"> 
 
    </script>

I를 지오 코딩 이 도움이되기를 바랍니다!