0

私のプロジェクトは 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);
});
4

1 に答える 1

1

ジオコーダー呼び出しでも関数クロージャーを使用する必要があります (テストされていません)。listMarker 関数にも問題があるようです (グローバルに依存している場合は、「info」の定義が欠落しているようです)その価値、それがあなたの問題かもしれません):

function geocodeAddress(msg)
{
            geocoder.geocode( { 'address': msg}, 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, msg);
                markerBounds.extend(results[0].geometry.location);
                map.fitBounds(markerBounds);
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

for (var i = 0; i < msg.length; i++) {
            info = msg[i];
            console.log(info);
            geocodeAddress(msg[i]);
}

function listenMarker (marker, info){
    google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(info);
    infoWindow.open(map, this);
});
于 2013-04-08T17:18:31.360 に答える