【问题标题】:Delete marker google map api. WHy show again marker when zoom?删除标记谷歌地图 api。为什么缩放时再次显示标记?
【发布时间】:2017-10-02 19:07:18
【问题描述】:

我在删除标记时遇到问题 我正确显示了所有位置,但是如果我单击具有删除标记功能的按钮,则在第一次删除但如果我移动地图或放大/缩小旧标记又来了....? 我只想删除

//show location
var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
     var icons = {
       parking: {
         icon: iconBase + 'parking_lot_maps.png'
       },
       library: {
         icon: iconBase + 'library_maps.png'
       },
       info: {
         icon: iconBase + 'info-i_maps.png'
       }
     };

var map;
var markers = [];

function initMap() {

    map = new google.maps.Map(document.getElementById('map'), {
      zoom: 5,
      center: {
        lat: -15.7942357,
        lng: -47.8821945
      }
    });

     var infoWin = new google.maps.InfoWindow();

     markers = locations.map(function(location, i) {
        var marker = new google.maps.Marker({
          position: location,
          icon: icons[location.type].icon,
        });
        google.maps.event.addListener(marker, 'click', function(evt) {
          infoWin.setContent(location.info);
          infoWin.open(map, marker);
        })
    return marker;
});


  // Add a marker clusterer to manage the markers.
  var markerCluster = new MarkerClusterer(map, markers, {
    imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
  });

}


// Sets the map on all markers in the array.
      function setMapOnAll(map) {
        for (var i = 0; i < markers.length; i++) {
          markers[i].setMap(map);
        }
      }

      // Removes the markers from the map, but keeps them in the array.
      function clearMarkers() {
        setMapOnAll(null);
      }

      // Shows any markers currently in the array.
      function showMarkers() {
        setMapOnAll(map);
      }

      // Deletes all markers in the array by removing references to them.
      function deleteMarkers() {
        clearMarkers();
        markers = [];
      }


var locations = [{
  lat: -19.9286,
  lng: -43.93888,
  info: "marker 1",
  type: 'info',
  category: 'cat1'
}, {
  lat: -19.85758,
  lng: -43.9668,
  info: "<strong>marker 2</strong><br>ciaociao",
  type: 'library',
  category: 'cat1'

}
, {
  lat: -18.85758,
  lng: -42.9668,
  info: "<strong>marker 3</strong><br>ciaociao",
  type: 'library',
  category: 'cat2'

} ];

【问题讨论】:

  • 你可以添加工作示例来显示问题
  • 解决了我已经发布了解决方案底部

标签: javascript google-maps google-maps-api-3


【解决方案1】:

问题是我必须首先填充数组 并在删除/隐藏之后 使用markers.push(marker); 并且不返回

locations.map(function(location, i) {
       var marker = new google.maps.Marker({
            position: location,
            icon: icons[location.type].icon,
            map: map
          });
        google.maps.event.addListener(marker, 'click', function(evt) {
          infoWin.setContent(location.info);
          infoWin.open(map, marker);
        })

    markers.push(marker);
});

这里有完整的代码

//show location
var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
     var icons = {
       parking: {
         icon: iconBase + 'parking_lot_maps.png'
       },
       library: {
         icon: iconBase + 'library_maps.png'
       },
       info: {
         icon: iconBase + 'info-i_maps.png'
       }
     };

var map;

var markers = [];
function initMap() {

    map = new google.maps.Map(document.getElementById('map'), {
      zoom: 5,
      center: {
        lat: -15.7942357,
        lng: -47.8821945
      }
    });

     var infoWin = new google.maps.InfoWindow();



      locations.map(function(location, i) {
       var marker = new google.maps.Marker({
            position: location,
            icon: icons[location.type].icon,
            map: map
          });
        google.maps.event.addListener(marker, 'click', function(evt) {
          infoWin.setContent(location.info);
          infoWin.open(map, marker);
        })

    markers.push(marker);
});


  // Add a marker clusterer to manage the markers.
  //var markerCluster = new MarkerClusterer(map, markers, {
   // imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
  //});

}


// Sets the map on all markers in the array.
      function setMapOnAll(map) {
        for (var i = 0; i < markers.length; i++) {
          markers[i].setMap(map);
        }
      }

      // Removes the markers from the map, but keeps them in the array.
      function clearMarkers() {
        setMapOnAll(null);
      }

      // Shows any markers currently in the array.
      function showMarkers() {
        setMapOnAll(map);
      }

      // Deletes all markers in the array by removing references to them.
      function deleteMarkers() {
        clearMarkers();
        markers = [];
      }


var locations = [{
  lat: -19.9286,
  lng: -43.93888,
  info: "marker 1",
  type: 'info',
  category: 'cat1'
}, {
  lat: -19.85758,
  lng: -43.9668,
  info: "<strong>marker 2</strong><br>ciaociao",
  type: 'library',
  category: 'cat1'

}
, {
  lat: -18.85758,
  lng: -42.9668,
  info: "<strong>marker 3</strong><br>ciaociao",
  type: 'library',
  category: 'cat2'

} ];

【讨论】:

    【解决方案2】:

    试试这个:

    function clearMarkers() {
      setMapOnAll(null);
      for(i=0; i<markers.length; i++){
         markers[i].setMap(null);
      }
    }
    

    来自:Google Map API - Removing Markers

    【讨论】:

      猜你喜欢
      • 2013-06-18
      • 2013-07-05
      • 2012-10-17
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 2018-10-13
      • 1970-01-01
      相关资源
      最近更新 更多