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

后端 未结 3 1055
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-06 02:07

Given a GMarker JS variable, how do I obtain the HTML DOM element that represents it? I need this so I can insert a

of my own into the map with the
3条回答
  •  不思量自难忘°
    2021-01-06 02:52

    This a simpler CustomMarker I'm using. Just provide a DOM element to it and it will be used as marker!

    // 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);
    };
    

    ~

提交回复
热议问题