从Google Map中删除单个标记-API v3 [英] Removing an individual marker from Google Map - API v3

查看:129
本文介绍了从Google Map中删除单个标记-API v3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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 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.

我的方法是对的吗?

推荐答案

您的问题是这一行:

mArray.push(markerInfo);

这不会将markerInfo的值压入您的数组中.它将对markerInfo的引用推入您的数组中.现在,在循环的下一个迭代中,当您更改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.

尝试以下方法:

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

如果那不起作用,那么这:

If that doesn't work, then this:

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

这篇关于从Google Map中删除单个标记-API v3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆