且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除谷歌地图位置服务中的标记

更新时间:2023-08-30 12:06:34

将标记保存在数组中。当您重新运行查询时,通过该数组进行处理,通过将标记的map属性设置为null来隐藏标记,然后将其删除。以下相关变更。

Save the markers in an array. When you re-run the query, process through that array, hiding the markers by setting their map property to null, then delete them. Relevant changes below.

var markers = [];
function createMarker(place)
{
    var placeLoc = place.geometry.location;
    marker = new google.maps.Marker({
        position: place.geometry.location
    });
    marker.setIcon({
        url:'bank.png',
        size: new google.maps.Size(70, 71),
        anchor: new google.maps.Point(17, 14),
        scaledSize: new google.maps.Size(35, 35)
    });

    marker.setMap(map);

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(place.name);
        infowindow.open(map, this);
    });
    markers.push(marker);
}

function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < markers.length; i++) {
       markers[i].setMap(null);
    }
    markers = [];
    for (var i = 0; i < results.length; i++) {
      createMarker(results[i]);
    }
  }
}

工作示例(不含您的自定义图标)