首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何清除google地图标记?

如何清除google地图标记?
EN

Stack Overflow用户
提问于 2019-05-21 23:35:25
回答 1查看 29关注 0票数 0

每次我的代码调用函数search()时,我希望将上一次搜索调用中放置在地图上的google地图标记从地图上清除,并将新的标记放置在地图上。我需要人帮我清理标记。

代码语言:javascript
复制
function search() {
//loading the map with markers
$('.map').addClass('.map-with-searching');
$.ajax({
  url: 'https://' + getApiURL(),
  data: data,
  contentType: 'application/json',
  type: 'POST',
  success: function (result) {
    $('#universitiesList').html('');
    for (const row of result.payload) {
      locations.push({ lat: row.location.latitude, lng: 
row.location.longitude, university: row.campusName, id:row.campusID});
    }
    //marker.setVisible(false);

    for (i = 0; i < locations.length; i++) {
      marker = new google.maps.Marker({
        position: { lat: locations[i].lat, lng: locations[i].lng },
        map: map,
        data: {
          university: locations[i].university,
          id: locations[i].id,
          name: locations[i].name,
          address: locations[i].address,
          image: locations[i].image,
        }
      });

      map.setCenter(new google.maps.LatLng(locations[i].lat, locations[i].lng));

      marker.addListener('click', function () {
        if (!this.infoWindow) {
          this.infoWindow = new google.maps.InfoWindow({
            content: '<div class="flex marker-pop-up"><div class="image-container"><img id="building" src="' 
            + this.data.image  + '"onerror="imgError(this)"; alt="Smiley face" height="110" width="120" /></div></div></div></div>'
          });
        }
        this.infoWindow.open(map, this);
      })
    };
  },
  error: function (jqXhr, textStatus, errorThrown) {
    //console.log(errorThrown);
  }
})
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56241769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档