【发布时间】:2011-04-13 12:53:21
【问题描述】:
我在从数组中删除标记时遇到了这个问题。我单击地图并在单击的位置放置标记,然后将标记保存在数组中。删除它们时,它只能按照我放置它们的顺序工作,但要向后,这意味着我放置 1 2 3 但必须像 3 2 1 一样删除它们。
如果我尝试以随机顺序删除标记,则删除第一个标记,但随后其他标记停止工作,侦听器仍然有效,但似乎 forloop 没有在数组中找到其他标记。
有什么想法吗?我完全迷路了。
代码如下:
var map;
var tempLatLng;
var zoomLevel;
var markers = [];
var zoomLevels = [];
var count = -1;
var nullLatlng = new google.maps.LatLng(84.52,45.16);
var nullMarker = new google.maps.Marker({
position: nullLatLng,
});
function initialize() {
var myLatlng = new google.maps.LatLng(55.605629745598904,13.000441789627075);
var myOptions = {
zoom: 17,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel:false
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
//Puts a listener on the map, when clicking on the map, it places a marker.
google.maps.event.addListener(map, 'click', function(event) {
zoomLevel = map.getZoom();
document.getElementById("zoom").value = zoomLevel;
tempLatLng = event.latLng;
setTimeout("placeMarker(tempLatLng)", 800); //placeMarker is called with a duration so that //doubleclicking doesn't bother the placement.
});
}
//Function to place markers.
function placeMarker(location) {
if(zoomLevel == map.getZoom()){
if(true){
var marker1 = new google.maps.Marker({
position: location,
map: map,
draggable:true
});
count = count + 1;
markers[count] = marker1;
document.getElementById("count").value = count;
google.maps.event.addListener(marker1,'rightclick', function(event){
document.getElementById("test2").value = "funkar";
for(var i = 0 ;i < markers.length ;i++){
if(markers[i].getTitle() == marker1.getTitle()){
marker1.setMap(null);
document.getElementById("markerpos").value = markers[i].getTitle();
document.getElementById("test1").value = markers[i].getTitle();
count = count - 1;
document.getElementById("count").value = count;
markers[i] = nullMarker;
}
}
});
marker1.setTitle(location.toString());
}
map.setCenter(location);
}
}
【问题讨论】:
-
您的删除标记基于右键单击标记本身。即使删除标记是随机顺序的,一切似乎都正常。 IE 创建标记 1 2 3,右键单击标记 2 将其删除,然后 1 3 也可以。
-
在您认为代码没有问题时会起作用吗?你试过了吗?我想把它放在服务器上,这样你们也可以试试。
-
@Denis 我现在看到了问题。如果我帮助您修复代码,如果它适合您,您会通过将复选标记标记到答案来接受答案吗?
-
当然是kjy!只需告诉我在代码修复后我在哪里执行此操作 :) (第一次使用 stackoverflow 用户)
-
@Denis 如果您有任何问题,请告诉我。要接受答案,请将鼠标悬停在上下箭头上,您应该会看到一个白色填充的复选标记。要接受,只需单击该复选标记。欢迎来到社区 =)
标签: google-maps google-maps-api-3