从 Google 地图中删除单个标记 - API v3

发布于 2024-11-16 06:05:09 字数 1157 浏览 2 评论 0原文

我想从 Google 地图中删除单个标记。我正在使用版本 3 API。我知道如何通过维护 markerArray 并将所有标记设置为 null 来删除所有标记。

为了一一删除,我正在考虑制作一个键值对组合。这样我就可以给出一个密钥并删除特定的标记。我需要帮助解决这个问题。

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

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提醒它。

我的做法是对的吗?

I want to remove an individual marker from Google map. I am using version 3 API. I know how I can remove all the markers by maintaining a markerArray and setting map null for all.

For removing one by one, I am thinking to make a key value pair combination. So that I give a key and remove the particular marker. I need help over this.

Following is the code, that I use to dram marker:

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);
            }
        });

    }
}

I will search for addresskey and remove the marker from mArray. But I get last value every time in geocode callback method. And one object got pushed every time. the var myLocation always give me the address of the last index of my array. If I alert it at check point A.

My approach is right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2024-11-23 06:05:09

您的问题是这一行:

mArray.push(markerInfo);

这不会将markerInfo的值推入您的数组中。它将对markerInfo 的引用推送到数组中。现在,在循环的下一次迭代中,当您更改markerInfo 的值时,它也会更改数组中引用指向的值。所以你的数组最终会有所有具有相同值的元素。

试试这个:

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

如果这不起作用,那么:

mArray.push({addressKey:data[i],marker:marker});

Your problem is this line:

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});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文