javascript谷歌地图api删除标记
我有一些函数,当单击li元素时,会将标记添加到地图中。如果单击另一个li,则删除原始标记并显示新标记 我遇到的问题是,当第一次单击li时,标记会放置在地图上。单击第二个li时,将删除标记,但不会添加新标记。我在firebug中没有错误。我看不出我错过了什么javascript谷歌地图api删除标记,javascript,google-maps,Javascript,Google Maps,我有一些函数,当单击li元素时,会将标记添加到地图中。如果单击另一个li,则删除原始标记并显示新标记 我遇到的问题是,当第一次单击li时,标记会放置在地图上。单击第二个li时,将删除标记,但不会添加新标记。我在firebug中没有错误。我看不出我错过了什么 $(document).ready(function() { $(".markerSelection").click(function() { var selectionId = $(this).attr("id");
$(document).ready(function() {
$(".markerSelection").click(function() {
var selectionId = $(this).attr("id");
drop(selectionId);
});
});
var markers = {
shopping : [
new google.maps.LatLng(52.26183, -7.11339),
new google.maps.LatLng(52.26134, -7.11226),
new google.maps.LatLng(52.26067, -7.11181),
new google.maps.LatLng(52.26003, -7.11033)],
cars : [
new google.maps.LatLng(52.26183, -7.11339),
new google.maps.LatLng(52.26134, -7.11226),
new google.maps.LatLng(52.26067, -7.11181),
new google.maps.LatLng(52.26003, -7.11033)]
};
var iterator = 0;
function drop(selectionId) {
clearOverlays();
for (var i = 0; i < markers[selectionId].length; i++) {
setTimeout(function() {
addMarker(selectionId);
}, i * 200);
}
}
function addMarker(selectionId) {
marker = new google.maps.Marker({
position: markers[selectionId][iterator],
map: map,
draggable: false,
animation: google.maps.Animation.DROP
});
iterator++;
markersArray.push(marker);
}
// Removes the overlays from the map, but keeps them in the array
function clearOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
}
}
$(文档).ready(函数(){
$(“.markerSelection”)。单击(函数(){
var selectionId=$(this.attr(“id”);
drop(selectionId);
});
});
变量标记={
购物:[
新google.maps.LatLng(52.26183,-7.11339),
新google.maps.LatLng(52.26134,-7.11226),
新的google.maps.LatLng(52.26067,-7.11181),
新google.maps.LatLng(52.26003,-7.11033)],
汽车:[
新google.maps.LatLng(52.26183,-7.11339),
新google.maps.LatLng(52.26134,-7.11226),
新的google.maps.LatLng(52.26067,-7.11181),
新google.maps.LatLng(52.26003,-7.11033)]
};
var迭代器=0;
函数下拉列表(selectionId){
clearOverlays();
对于(var i=0;i
您已经将标记定义为Json变量,但我不知道您所说的标记[selectionId]
是什么意思!标记没有定义为数组,按索引引用它似乎是不正确的 我再次查看了您的代码,我认为问题在于迭代器
,它在全局范围内初始化为0。这就是为什么它第一次工作正常,但之后,指数超过。似乎应该在drop()函数的开头将其设置为零
但是,如果您将索引作为addMarker()
的第二个参数传递,而不是在drop()
中处理的外部变量,并使代码复杂化,则更有意义。我建议您发布一个显示问题的实例,可能是一个小问题。这篇文章和其他文章有什么不同?在我之前的问题中,我在问传递字符串并基于此获取变量的最佳方法是什么。一个答案是使用一个你可以看到我已经实现的对象。上面的代码在一定程度上是有效的,我想可能有一些非常明显的地方我遗漏了。我将看看如何制作一个JSFIDLE版本,thanksmarkers是一个对象,我通过selectionIdYes(你是对的)将购物或汽车传递给它;)我不知道为什么我假设selectionId是一个数字!非常感谢Alireza的帮助。有目共睹