Google Maps: Event Listener merkt sich nur den Endwert der Variablen

Ich stelle eine Google Map zusammen, die die Standorte verschiedener Testzentren in meinem Land enthält. Es zeichnet eine Markierung für jede Grafschaft auf. Wenn Sie auf die Grafschaftsmarkierung klicken, wird die Ansicht vergrößert und eine Übersicht über die Testzentren in dieser Grafschaft angezeigt. Ich benutze auch jQuery damit.

Hier ist das Problem:

Wenn ich die County-Marker zeichne und darauf klicke, wird immer zum letzten County gezoomt. Der Code, mit dem ich die Counties zeichne, lautet wie folgt:

function plotCountyMarkers(county_count)
{
    // Setup a new icon
    var icon = new GIcon();
    var count = 0;

    // Get the type of icon to display
    var centre_type = checkCentreType();
    if (centre_type == 'dtc')
        icon.image = dtc_icon;
    else
        icon.image = ctc_icon;

    // Other settings including icon shadow
    icon.shadow = icon_shadow;
    icon.iconSize = new GSize(20, 29);
    icon.shadowSize = new GSize(38, 29);
    icon.iconAnchor = new GPoint(10, 29);
    icon.infoWindowAnchor = new GPoint(10, 1);

    // Get the total number of counties to map
    var count = county_count.length;

    for (key in county_count) {
        // Set the LatLong of the county
        var countyLocation = new GLatLng(county_locations[key][0],county_locations[key][1]);
        // Set the title text of the marker
        var centre_text = county_count[key]==1 ? 'Centre' : 'Centres';
        var title = county_locations[key][2]+': '+county_count[key]+' Test '+centre_text;
        // Add an event listener to the marker
        var marker = new GMarker(countyLocation,{icon: icon, title: title});

        GEvent.addListener(marker, "click", function() {
            // Zoom to county
            showCounty(key);
        });

        // Add the marker to the map
        map.addOverlay(marker);
    }
}

Ich benutze im Grunde genau die gleiche Methode, um HTML an einen Ereignis-Listener zu übergeben, wenn Sie auf die Markierungen auf Kreisebene klicken, und das funktioniert einwandfrei. Aus irgendeinem Grundkey ist immer der Wert der endgültigen Grafschaft. Ich habe versucht vorbei zu kommenkey als Variable für die Funktion, wird jedoch nur gleich dem Längen- und Breitengrad der aktuellen Kartenposition.

Vielleicht mache ich etwas Idiotisches? Es wäre nicht das erste Mal :) Jede Hilfe wäre sehr dankbar.

Antworten auf die Frage(1)

Ihre Antwort auf die Frage