Get DOM Element of a marker in Google Maps API 3

前端 未结 5 1024
野性不改
野性不改 2020-12-15 04:16

I\'m trying to find a way to get the DOM element of a marker. It seems that Google uses different elements for showing a marker and one of handling the event.

I\'ve

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-15 05:05

    I was looking for the DOM element too in order to implement a custom tooltip. After a while digging into google overlays, custom libraries and so on, i've ended up with the following solution based on fredrik's title approach (using jQuery) :

    google.maps.event.addListener(marker, 'mouseover', function() {
    
        if (!this.hovercardInitialized) {
    
            var markerInDOM = $('div[title="' + this.title + '"]').get(0);
    
            // do whatever you want with the DOM element
    
            this.hovercardInitialized = true;
        }
    
    });
    

    Hope this helps someone.

提交回复
热议问题