Disable click behavior of POI markers in Google Maps JS API

前端 未结 5 862
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-06 10:17

As of Google Maps API v3.6, maps now include \"points of interest\", which are gray markers embedded into a map. When the user clicks on this icon, an InfoWindow appears wi

5条回答
  •  一整个雨季
    2020-12-06 11:12

    By referencing this URL (https://stackoverflow.com/a/24234818/6160436), I somehow managed to hide the Info windows of POI and call the click event listener of map when the user clicks on the POI. But I'm not sure whether this violates TOS or not, so use at your own risk.

            //keep a reference to the original setPosition-function
            var fx = google.maps.InfoWindow.prototype.setPosition;
    
            //override the built-in setPosition-method
            google.maps.InfoWindow.prototype.setPosition = function () {
    
                //this property isn't documented, but as it seems
                //it's only defined for InfoWindows opened on POI's
                if (this.logAsInternal) {
                    if (this.getPosition()) {
                        console.log(this.getPosition());
                        google.maps.event.trigger(map, 'click', {latLng: this.getPosition()});
                    }
                    else{
                        google.maps.event.addListenerOnce(this, 'map_changed',function () {
                            console.log(this.getPosition());
                            google.maps.event.trigger(map, 'click', {latLng: this.getPosition()});
    
                            // var map = this.getMap();
                            // //the infoWindow will be opened, usually after a click on a POI
                            // if (map) {
                                //trigger the click
    
                                var removeInfoWindow = null;
    
                                removeInfoWindow = setInterval(function(){
                                    if ($('.gm-style-iw').parent().length) {
                                        $('.gm-style-iw').parent().hide();
                                        clearInterval(removeInfoWindow);
                                    };
                                },1);
                            // }
                        });
                    };
                }
    
                //call the original setPosition-method
                fx.apply(this, arguments);
            };
    
            google.maps.event.addListener(map,'click',function(e){
                alert('clicked @'+e.latLng.toString())
                console.log('ok');
            });
    

提交回复
热议问题