谷歌地图API v3如何在点击的标记上添加阴影

4
我知道在谷歌地图API的v3版本中,官方已经移除了标记的阴影。考虑到这一点,我有一个需要使用标记阴影的项目。当标记被点击时,我想在标记上放置一个阴影。这实质上是向标记添加事件监听器,并在单击标记时向标记添加阴影,以显示所单击的标记是活动标记。
我阅读了一些页面,例如谷歌地图v3中的标记阴影,其中所有标记都带有阴影,但我想借鉴这样的示例,并在单击标记时添加阴影,并在标记失去焦点(即单击另一个标记时)时删除阴影。
我的jsfiddle有两个标记可供使用,代码如下:jsfiddle在此处
var marker;
var locations = [["6","43.683","9.58","3002: Location 1",1],["7","45.149","9.44","3003: Location",2]];
function initialize() {
    var mapProp = {
    center: new google.maps.LatLng(43.683, 9.44),
    zoom: 5,
    mapTypeId: google.maps.MapTypeId.ROADMAP};

    var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
        for (i = 0; i < locations.length; i++) {
        marker = new google.maps.Marker({
        position:new google.maps.LatLng(locations[i][1], locations[i][2]),
        icon:'https://maps.gstatic.com/mapfiles/ms2/micons/purple.png',
    });

    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            alert(locations[i][3] + " was clicked");
        }
    })(marker, i));

    marker.setMap(map);
    }
}

google.maps.event.addDomListener(window, 'load', initialize);

如果有人能帮忙提供一个策略或者代码片段,让点击标记时产生阴影,我会非常高兴。请随意 fork 这个 jsfiddle,并将修改后的链接放在这里。

1个回答

3
另一个选项是创建标记阴影对象(来自您所引用的问题的答案),第一次单击标记时,将其移动到新单击的标记(向MarkerShadow类添加.setPosition方法)。

var marker;
var locations = [
  ["6", "43.683", "9.58", "3002: Location 1", 1, true],
  ["7", "45.149", "9.44", "3003: Location", 2, false]
];
var markerShadow;
var infowindow = new google.maps.InfoWindow();

function initialize() {
  var mapProp = {
    center: new google.maps.LatLng(43.683, 9.44),
    zoom: 5,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
  var iconShadow = {
    url: 'http://www.geocodezip.com/mapIcons/marker_shadow.png',
    // The shadow image is larger in the horizontal dimension
    // while the position and offset are the same as for the main image.
    size: new google.maps.Size(37, 34),
    origin: new google.maps.Point(0, 0),
    anchor: new google.maps.Point(10, 34)
  };


  for (i = 0; i < locations.length; i++) {
    marker = new google.maps.Marker({
      position: new google.maps.LatLng(locations[i][1], locations[i][2]),
      icon: 'https://maps.gstatic.com/mapfiles/ms2/micons/purple.png',
      title: locations[i][3]
    });
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
      return function() {
        if (markerShadow && markerShadow.setPosition) {
          markerShadow.setPosition(this.getPosition());
        } else if (!markerShadow) {
          markerShadow = new MarkerShadow(marker.getPosition(), iconShadow, map);
        }
      }
    })(marker, i));

    marker.setMap(map);
  }
}

google.maps.event.addDomListener(window, 'load', initialize);

// marker shadow code
MarkerShadow.prototype = new google.maps.OverlayView();
MarkerShadow.prototype.setPosition = function(latlng) {
    this.posn_ = latlng;
    this.draw();
  }
  /** @constructor */

function MarkerShadow(position, options, map) {

    // Initialize all properties.
    this.posn_ = position;
    this.map_ = map;
    if (typeof(options) == "string") {
      this.image = options;
    } else {
      this.options_ = options;
      if (!!options.size) this.size_ = options.size;
      if (!!options.url) this.image_ = options.url;
    }

    // Define a property to hold the image's div. We'll
    // actually create this div upon receipt of the onAdd()
    // method so we'll leave it null for now.
    this.div_ = null;

    // Explicitly call setMap on this overlay.
    this.setMap(map);
  }
  /**
   * onAdd is called when the map's panes are ready and the overlay has been
   * added to the map.
   */
MarkerShadow.prototype.onAdd = function() {
  // if no url, return, nothing to do.
  if (!this.image_) return;
  var div = document.createElement('div');
  div.style.borderStyle = 'none';
  div.style.borderWidth = '0px';
  div.style.position = 'absolute';

  // Create the img element and attach it to the div.
  var img = document.createElement('img');
  img.src = this.image_;
  img.style.width = this.options_.size.x + 'px';
  img.style.height = this.options_.size.y + 'px';
  img.style.position = 'absolute';
  div.appendChild(img);

  this.div_ = div;

  // Add the element to the "overlayLayer" pane.
  var panes = this.getPanes();
  panes.overlayShadow.appendChild(div);
};

MarkerShadow.prototype.draw = function() {
  // if no url, return, nothing to do.
  if (!this.image_) return;
  // We use the coordinates of the overlay to peg it to the correct position 
  // To do this, we need to retrieve the projection from the overlay.
  var overlayProjection = this.getProjection();

  var posn = overlayProjection.fromLatLngToDivPixel(this.posn_);

  // Resize the image's div to fit the indicated dimensions.
  if (!this.div_) return;
  var div = this.div_;
  if (!!this.options_.anchor) {
    div.style.left = Math.floor(posn.x - this.options_.anchor.x) + 'px';
    div.style.top = Math.floor(posn.y - this.options_.anchor.y) + 'px';
  }
  if (!!this.options_.size) {
    div.style.width = this.size_.x + 'px';
    div.style.height = this.size_.y + 'px';
  }
};

// The onRemove() method will be called automatically from the API if
// we ever set the overlay's map property to 'null'.
MarkerShadow.prototype.onRemove = function() {
  if (!this.div_) return;
  this.div_.parentNode.removeChild(this.div_);
  this.div_ = null;
};
html,
body,
#googleMap {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="googleMap"></div>

展示结果的代码片段

生成地图的截图


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接