重置Google Maps v3 Javascript中的标记图标

时间:2018-08-16 16:01:13

标签: google-maps-api-3

我正在使用以下方式更改当前所选标记的图标:

var icon_active = {
    url: '/2018/images/map/location.png',
    size: new google.maps.Size(32, 32),
    origin: new google.maps.Point(0, 0),
    anchor: new google.maps.Point(16, 42)
};
markers[window.VARS.marker_currently_open].setIcon(icon_active);

我遇到的问题是如何将其重置为以前的状态?我试图找到标准图标的列表,即:

enter image description here

有人可以建议我这样做吗?我试图找到的是可用的不同(默认)图标的列表,并找到标准图标?

1 个答案:

答案 0 :(得分:3)

要设置默认图标,请致电marker.setIcon(null)

即:

markers[window.VARS.marker_currently_open].setIcon(null);

proof of concept fiddle

代码段:

var markers = [];

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var marker = new google.maps.Marker({
    position: map.getCenter(),
    map: map
  })
  markers.push(marker);
  window.VARS = {
    marker_currently_open: 0
  }
  var icon_active = {
    url: 'http://maps.google.com/mapfiles/ms/micons/blue.png',
    size: new google.maps.Size(32, 32),
    origin: new google.maps.Point(0, 0),
    anchor: new google.maps.Point(16, 42)
  };
  markers[window.VARS.marker_currently_open].setIcon(icon_active);
  setTimeout(function() {
    markers[window.VARS.marker_currently_open].setIcon(null);
  }, 5000);
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>