谷歌地图和阿贾克斯我可以添加标记,但我无法删除或隐藏它们

google maps and ajax i can add the markes but i am not able to delete or hide them

本文关键字:删除 隐藏 阿贾克斯 我可以 添加 加标记 谷歌地图      更新时间:2023-09-26
function checkPostServer(){
            $.ajax({
                type : "POST",
                url : "/getIngo/",
                data : {
                    msg: chkToServer
                },
                 dataType: 'json',

                async: false,
                success: function(data) {
                    disMarker(data)
                }
            });
}
function disMarker(data){

     if (data) {
         $.each(data, function (i, item) {
             createMarker(item);
         });
     }
}

function createMarker(data){
                     var latLng = new google.maps.LatLng(data.lat, data.lng);
                    // ...and add the Marker to your map
                    var marker = new google.maps.Marker({
                        position: latLng,
                        map: map
                    });

                    //console.log(data);
                    markers2[data.key] = marker;
                    marker.setMap(map);
}
}, 2000);
setInterval(function() {
 markers2[2].setMap(null);
 alert("Here");
}, 4000);

你应该做这样的事情

var markers = [];  // this variable must be within a scope where the functions can read it (for example global scope)
// ...
function disMarker(data) {
  if (data) {
    // first clear all markers
    clearMarkers();
    $.each(data, function (i, item) {
      createMarker(item);
    });
  }
}
function createMarker(data) {
  var latLng = new google.maps.LatLng(data.lat, data.lng);
  var marker = new google.maps.Marker({
    position: latLng,
    map: map            // this line replaces marker.setMap(map);  you don't need both
  });
  // now we add this object to the markers array
  markers.push(marker);
}
function clearMarkers() {
  for (var i=0; i<markers.length; i++) {
    markers[i].setMap(null);            // this removes the marker from the map
  }
  markers = [];  // now this variable is an empty array again; and we can fill it with new marker-orbects 
}

我不知道标记2是关于什么的;我不使用它