2017-12-01 6 views
-1

約60のアドレスをループして、Googleマップで使用するためにジオコードされています。私のコールバック(下記)は場所を収集するためにはうまくいくようですが、私がループしているアドレスオブジェクトにそれらを関連付ける方法を知る必要があります。ジオコードレスポンスで、私の要求のどれが「どこから来たのか」を知ることができません。ジオコーダージオコードコールバックでは、結果がどの要求に対応しているかをどのように判断するのですか?

これを行う方法はありますか?

これは私の関数である:

あなたも閉鎖として知られている関数スコープを作成します Immediately-invoked function expressionを使用することができますJavaScriptで
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

。あなたは

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をジオコーディングお役に立てれば!

関連する問題