Disable click behavior of POI markers in Google Maps JS API

前端 未结 5 919
佛祖请我去吃肉
佛祖请我去吃肉 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 10:49

    I'm not sure if this is still relevant to you, but Google did, indeed, solve the issue on April, 2016, all you need to do is clickableIcons to false in MapOptions

    0 讨论(0)
  • 2020-12-06 11:11

    A couple of things to be aware of:

    1) If your map is high traffic, you may find yourself in violation of the Google Maps TOS. You're supposed to use an official version. If it's your own blog or something else low traffic, nobody will notice or care.

    2) This is only conjecture on my part, but I noticed these POI's myself and got annoyed by them. I am pretty sure these are paid-for "inline ads", so to speak. Some gas stations and diner chains have them, so you will soon see Google maps being spammed with these POI markers. If they allow those to be turned off in the API, it kind of goes against the business interests of those who paid for the POI icon... so I highly doubt that you will be able to remove them.

    If you do find a way,please please DO POST the solution! Thanks.

    0 讨论(0)
  • 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');
            });
    
    0 讨论(0)
  • 2020-12-06 11:14

    This issue has been logged with google at:

    http://code.google.com/p/gmaps-api-issues/issues/detail?id=3866

    Please star and comment your needs on this issue there.

    0 讨论(0)
  • 2020-12-06 11:16

    I'm not sure this is not a violation of the Google Maps TOS, it's a bit hacky, and doesn't work on IE < 9, but you can listen on dom event, to detect the creation of the window, using Mutation Observer

    Here is a plunkr to demonstrate : http://plnkr.co/edit/pGep9OZFligLhRtHlhgk You can check in the console, an event is fired (actually twice) when you click on a POI, and the window is hidden

    0 讨论(0)
提交回复
热议问题