从 Google 地图中删除单个标记 - API v3
我想从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是这一行:
这不会将markerInfo的值推入您的数组中。它将对markerInfo 的引用推送到数组中。现在,在循环的下一次迭代中,当您更改markerInfo 的值时,它也会更改数组中引用指向的值。所以你的数组最终会有所有具有相同值的元素。
试试这个:
如果这不起作用,那么:
Your problem is this line:
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:
If that doesn't work, then this: