简体   繁体   English

如何获取Google Maps标记的HTML DOM元素?

[英]How to obtain the HTML DOM element of a Google Maps marker?

Given a GMarker JS variable, how do I obtain the HTML DOM element that represents it? 给定一个GMarker JS变量,我如何获得代表它的HTML DOM元素? I need this so I can insert a <div> of my own into the map with the correct z-index. 我需要这个,所以我可以使用正确的z-index将我自己的<div>插入到地图中。

Thanks. 谢谢。

Sorry to post on such an old question, but I've just come across this myself. 很抱歉发布这么老的问题,但我自己也遇到过这个问题。 The solution I used in Google Maps APIv3 was to copy the "Custom Marker" from the Google Maps samples and add a simple method getDOMElement , which returns the div generated in the Marker's construction. 我在Google Maps APIv3中使用的解决方案是从Google Maps示例中复制“自定义标记”,并添加一个简单的方法getDOMElement ,它返回Marker构造中生成的div

CustomMarker.prototype.getDOMElement = function() {
  return this.div_;
}

You can then use marker.getDOMElement().style to dynamically change the styling of your marker, and the img child element of marker.getDOMElement() is the icon used, so you can change that dynamically too. 然后,您可以使用marker.getDOMElement().style来动态更改标记的样式,并且marker.getDOMElement()img子元素是使用的图标,因此您也可以动态更改它。

It looks like the Google Maps API doesn't provide a method to return a marker's DOM element. 看起来Google Maps API不提供返回标记DOM元素的方法。

Do you just want to create your own custom marker? 您只是想创建自己的自定义标记吗? If so, you can create a marker class which extends GOverlay. 如果是这样,您可以创建一个扩展GOverlay的标记类。 MarkerLight is a great example of how to accomplish this (and here is the example page ). MarkerLight是如何实现这一目标的一个很好的例子(这里是示例页面 )。

If all you need is a custom icon, here is how to do that. 如果你需要的只是一个自定义图标, 这里是如何做到这一点。

This a simpler CustomMarker I'm using. 这是我正在使用的更简单的CustomMarker。 Just provide a DOM element to it and it will be used as marker! 只需提供一个DOM元素,它就会被用作标记!

// based on http://gmaps-samples-v3.googlecode.com/svn/trunk/overlayview/custommarker.html

function CustomMarker(options) {
  this.options = options;
  this.element = options.element;
  this.map = options.map;
  this.position = options.position;
  this.positionFunction = options.positionFunction || function () {
    var point = this.getProjection().fromLatLngToDivPixel(this.position);
    if (point) {
      this.element.style.position = 'absolute';
      this.element.style.left = (point.x - jQuery(this.element).width()/2) + 'px';
      this.element.style.top = (point.y - jQuery(this.element).height()) + 'px';
      this.element.style.cursor = 'pointer';
    }
  };

  this.setMap(this.map);
}

CustomMarker.prototype = new google.maps.OverlayView();
CustomMarker.prototype.draw = function() {
  if (!this.div_)
    this.getPanes().overlayImage.appendChild(this.element);
  this.positionFunction();
};
CustomMarker.prototype.getPosition = function() {
  return this.position;
};
CustomMarker.prototype.setVisible = function(bool) {
  jQuery(this.element).toggle(bool);
};

~

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM