私のプロジェクトは eBay API を検索し (PHP を使用して simpleXML を返します)、複数のアイテム (現時点では 5 つ) の郵便番号を返します。次に、この情報を使用して、私のウェブサイトの Google マップにマーカーをプロットします。私がやろうとしているのは、これらのマーカーと一緒に複数の情報ウィンドウを作成して、eBay オークションから情報を返し、それを情報ウィンドウ (オークションへのリンク、アイテムの画像など) に配置できるようにすることですが、うまくいきません! ループでクロージャーを正しく取得できないようで、実際にそのマーカーに関連付けられている郵便番号ではなく、情報ウィンドウに表示される配列の最後の郵便番号を取得し続けます (テスト目的でこれを行うだけです)。
私は何を間違っていますか?どんな情報も役に立ちます。
これは現時点での私のコードです:
for (var i = 0; i < msg.length; i++) {
info = msg[i];
console.log(info);
geocoder.geocode( { 'address': msg[i]}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: map,
animation: google.maps.Animation.DROP,
icon: image,
position: results[0].geometry.location
})
listenMarker(marker);
markerBounds.extend(results[0].geometry.location);
map.fitBounds(markerBounds);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
function listenMarker (marker){
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(info);
infoWindow.open(map, this);
});