0

我想从谷歌地图中删除一个单独的标记。我正在使用版本 3 API。我知道如何通过为所有人维护markerArray和设置地图为空来删除所有标记。

为了一一删除,我正在考虑进行键值对组合。这样我就给了一个钥匙并删除了特定的标记。我需要帮助。

以下是我用来标记标记的代码:

function geoCodeAddresses(data) {

    var markerInfo = {addressKey: '', marker:''};

    for (var i = 0; i < data.length; i++) {
        myLocation = data[i];

        geocoder.geocode({"address":myLocation}, function (results, status) {

            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({map:map, position:results[0].geometry.location});
                // checkpoint A
                alert(myLocation);
                /*
                markerInfo.addressKey = myLocation;
                markerInfo.marker = marker;*/

                //mArray.push(markerInfo);
            }
        });

    }
}

我将从中搜索addresskey并删除标记mArray。但是我每次在地理编码回调方法中都得到最后一个值。每次都推一个对象。var myLocation 总是给我数组最后一个索引的地址。如果我在检查点 A 提醒它。

我的做法对吗?

4

1 回答 1

0

你的问题是这一行:

mArray.push(markerInfo);

That doesn't push the values of markerInfo into your array. It pushes a reference to markerInfo into your array. Now, on your next iteration of the loop, when you change the value of markerInfo, it changes the value pointed at by the references in the array too. So your array ends up having elements that all have the same value.

Try this instead:

mArray.push({addressKey:myLocation,marker:marker});

If that doesn't work, then this:

mArray.push({addressKey:data[i],marker:marker});
于 2011-06-18T22:20:13.023 回答