javascript - 设置后如何删除谷歌地图API标记

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

使用我的代码,每次单击 map 时都会出现几个标记。

我只想一个标记,每次单击都会删除前一个标记并将新标记的纬度/经度设置为新位置。

这是我的代码:

  var map;
  var markers = [];

  function initMap() {
      var aa = {lat: 32.3896651, lng: 48.3791718};

    map = new google.maps.Map(document.getElementById('map'), {
      zoom: 12,
      center: aa
    });

    // This event listener will call addMarker() when the map is clicked.
    map.addListener('click', function(event) {
      addMarker(event.latLng);
      document.getElementById("la").value = event.latLng.lat();
 document.getElementById("lo").value = event.latLng.lng();
    });



  }

  // Adds a marker to the map and push to the array.
  function addMarker(location) {
    var marker = new google.maps.Marker({
      position: location,
    flat: false,
    map: map,
    draggable: true
    });
    markers.push(marker);
  }

  // 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 marker = null;

然后使用setPosition更改现有标记对象的位置:

marker.setPosition(location);

这样,您只需在不同的位置使用一个标记。

 var map;
 var marker = null;

function initMap() {
     var aa = {lat: 32.3896651, lng: 48.3791718};

     map = new google.maps.Map(document.getElementById('map'), {
       zoom: 12,
       center: aa
     });

// This event listener will call addMarker() when the map is clicked.
     map.addListener('click', function(event) {
         addMarker(event.latLng);
         document.getElementById("la").value = event.latLng.lat();
         document.getElementById("lo").value = event.latLng.lng();
     });

   }

// Adds a marker to the map and push to the array.
function addMarker(location) {
        if(marker)
            marker.setPosition(location);
        else
            marker = new google.maps.Marker({
                position: location,
                flat: false,
                map: map,
                draggable: true
        });

    }

 // Sets the map on all markers in the array.
 function setMapOnAll(map) {

      marker.setMap(map);

  }

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

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

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

关于javascript - 设置后如何删除谷歌地图API标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43464316/

相关文章:

javascript - JSONP 和 Google map 方向 API

javascript - 谷歌API : How to pass zip-code value into the JavaScript code

javascript - 单击 TAB 时,焦点应位于下一个文本框上

iphone - MKMapView 一次加载所有注释 View (包括那些在当前矩形之外的 View )

php - 从 Gmaps 中获取草地、街道、水等信息

javascript - setMap 不起作用

google-maps - Google Maps API v3 - 如何检测 map 何时更改为全屏模式?

javascript - 表格/嵌套(?)JSON 集合,然后在服务器端访问它

javascript - 当页面上有多个相同的 ajax 调用时,如何加快我的 Ajax 调用速度?

JavaScript 在对象数组中查找匹配对象