简体   繁体   中英

Google Maps API V3 Event

I'm wanting this code

   google.maps.event.addListener(map, 'click', function(event) {
      var marker = new google.maps.Marker({
      position: event.latLng
   }); 
      var outsideContent = "my content";
       infowindow.setContent(outsideContent);
      infowindow.open(map, marker);
      });

to open an info window if the map is clicked where an overlay/polygon is not present. However, the code isn't executing.

Javascript is case-sensitive. infoWindow and infowindow refer to two different things.

infoWindow.setContent(outsideContent);
infowindow.open(map, marker);

Not sure this is actually your problem though (I'd expect to still see a blank infowindow). Of course you don't need to have a marker to open an infowindow, eg check this blog post of mine showing how to add an infowindow to a map in response to user clicks .

In theory this should work for you:

<script type="text/javascript">
function initialize() {
    var myOptions = {
        zoom: 10,
        center: new google.maps.LatLng(50.820645,-0.137376),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var infowindow =  new google.maps.InfoWindow({
        content: 'my content'
    });

    google.maps.event.addListener(map, 'click', function(event) {
        infowindow.setPosition(event.latLng);
        infowindow.open(map);
    }); 
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>

The problems might stem from any other event listeners you have, and possibly the KML layers you've got going on.

It looks like you have a problem with the location where you have defined your map click listener, it is down inside the generateInfoWindow function, which only appears to get called from the zoomtoaddress function.

If you move that listener setup out next to where you create the map in the intialize function, I think you should be good with opening the InfoWindow when the map is clicked:

function initialize() {
    // ...
    map = new google.maps.Map(document.getElementById('map_canvas'), options);

    // Move your event listener definition right here:
    google.maps.event.addListener(map, 'click', function(event) {
        var marker = new google.maps.Marker({ position: event.latLng }); 
        var outsideContent = "outside content replaced for brevity";
        infowindow.setContent(outsideContent);    //include var name correction
        infowindow.open(map, marker);
    });

    //the rest of initialize...
}

Also, if you want the marker you are creating in the event listener to display, you will have to pass the map property of the MarkerOptions as well. It looks like you may not want the marker to display, so I've left that out -

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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