Google maps 如何在html中编辑/删除google地图中的标记

Google maps 如何在html中编辑/删除google地图中的标记,google-maps,geolocation,Google Maps,Geolocation,我有一个html页面,在谷歌地图上显示地址点。 我的地理点将每20秒刷新一次,我希望删除旧位置并标记新地理位置: 我的html和java脚本代码是: <script src="http://maps.google.com/maps/api/js?sensor=false"></script> <script> var markers = []; var locations = [ ['<h4>Bondi Beach</h4&

我有一个html页面,在谷歌地图上显示地址点。 我的地理点将每20秒刷新一次,我希望删除旧位置并标记新地理位置: 我的html和java脚本代码是:

 <script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
  var markers = [];
  var locations = [
    ['<h4>Bondi Beach</h4>', -33.890542, 151.274856],
    ['<h4>Coogee Beach</h4>', -33.923036, 151.259052],
    ['<h4>Cronulla Beach</h4>', -34.028249, 151.157507],
    ['<h4>Manly Beach</h4>', -33.80010128657071, 151.28747820854187],
    ['<h4>Maroubra Beach</h4>', -33.950198, 151.259302]
  ];
  var iconURLPrefix = 'http://maps.google.com/mapfiles/ms/icons/';
  var icons = [
    iconURLPrefix + 'red-dot.png',
    iconURLPrefix + 'green-dot.png',
    iconURLPrefix + 'blue-dot.png',
    iconURLPrefix + 'orange-dot.png',
    iconURLPrefix + 'purple-dot.png',
    iconURLPrefix + 'pink-dot.png',
    iconURLPrefix + 'yellow-dot.png'
  ]
  var iconsLength = icons.length;

  var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(-37.92, 151.25),
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      mapTypeControl: false,
      streetViewControl: false,
      panControl: false,
      zoomControlOptions: {
          position: google.maps.ControlPosition.LEFT_BOTTOM
      }
  });
  var infowindow = new google.maps.InfoWindow({
      maxWidth: 160
  });
  var iconCounter = 0;
  for (var i = 0; i < locations.length; i++) {
      var marker = new google.maps.Marker({
          position: new google.maps.LatLng(locations[i][1], locations[i][2]),
          map: map,
          icon: icons[iconCounter]
      });

      markers.push(marker);

      google.maps.event.addListener(marker, 'click', (function (marker, i) {
          return function () {
              infowindow.setContent(locations[i][0]);
              infowindow.open(map, marker);
          }
      })(marker, i));

      iconCounter++;
      if (iconCounter >= iconsLength) {
          iconCounter = 0;
      }
  }

  function autoCenter() {
      var bounds = new google.maps.LatLngBounds();
      for (var i = 0; i < markers.length; i++) {
          bounds.extend(markers[i].position);
      }
      map.fitBounds(bounds);
  }
  autoCenter();
  ///////////////////////////////////////////////////////
  setInterval(function () {
      markers = null;
      locations = [
        ['<h4>Bondi Beach</h4>', -34.890542, 151.274856],
        ['<h4>Coogee Beach</h4>', -33.923036, 151.259052],
        ['<h4>Cronulla Beach</h4>', -34.028249, 151.157507],
        ['<h4>Manly Beach</h4>', -33.80010128657071, 151.28747820854187],
        ['<h4>Maroubra Beach</h4>', -33.950198, 151.259302]
      ];
      icons = [
        iconURLPrefix + 'red-dot.png',
        iconURLPrefix + 'green-dot.png',
        iconURLPrefix + 'blue-dot.png',
        iconURLPrefix + 'orange-dot.png',
        iconURLPrefix + 'purple-dot.png',
        iconURLPrefix + 'pink-dot.png',
        iconURLPrefix + 'yellow-dot.png'
      ];
      iconsLength = icons.length;

      ///////////////////////////////////////////////////
      for (var i = 0; i < locations.length; i++) {
          var marker = new google.maps.Marker({
              position: new google.maps.LatLng(locations[i][1], locations[i][2]),
              map: map,
              icon: icons[iconCounter]
          });
          markers.push(marker);
          google.maps.event.addListener(marker, 'click', (function (marker, i) {
              return function () {
                  infowindow.setContent(locations[i][0]);
                  infowindow.open(map, marker);
              }
          })(marker, i));
          iconCounter++;
          if (iconCounter >= iconsLength) {
              iconCounter = 0;
          }
      }
  }, 20000);

var标记=[];
变量位置=[
[Bondi Beach',-33.890542151.274856],
[Coogee Beach',-33.923036151.259052],
['Cronulla Beach',-34.028249151.157507],
[‘曼利海滩’,-33.80010128657071151.28747820854187],
[‘马鲁布拉海滩’,-33.950198151.259302]
];
变量iconURLPrefix=http://maps.google.com/mapfiles/ms/icons/';
变量图标=[
iconURLPrefix+“red dot.png”,
iconURLPrefix+“green dot.png”,
iconURLPrefix+“blue dot.png”,
iconURLPrefix+“orange dot.png”,
iconURLPrefix+“purple dot.png”,
iconURLPrefix+“pink dot.png”,
iconURLPrefix+“黄点.png”
]
var iconsLength=icons.length;
var map=new google.maps.map(document.getElementById('map'){
缩放:10,
中心:新google.maps.LatLng(-37.92151.25),
mapTypeId:google.maps.mapTypeId.ROADMAP,
mapTypeControl:false,
街景控制:错误,
泛控制:错误,
ZoomControl选项:{
位置:google.maps.ControlPosition.LEFT_底部
}
});
var infowindow=new google.maps.infowindow({
最大宽度:160
});
var-iconCounter=0;
对于(变量i=0;i=iconsLength){
i计数器=0;
}
}
函数autoCenter(){
var bounds=new google.maps.LatLngBounds();
对于(var i=0;i=iconsLength){
i计数器=0;
}
}
}, 20000);

我在您的代码中发现一个javascript错误
未捕获的TypeError:无法读取null的属性“push”
。您正在将标记数组设置为
null
,之后无法在其上推送新标记

解决这个问题。要删除标记,请在每个标记上重复调用.setMap(null)数组以将其从映射中删除,然后将数组设置为空(
markers=[];



运行setTimeOut时我希望首先清除所有标记(点)并显示新位置时,您遇到的问题是什么
  for (var i=0; i<markers.length;i++) {
      markers[i].setMap(null);
  }
  markers = [];