Google Maps Events

Click the marker to zoom - attach event handlers to Google maps.

Click The Marker to Zoom

We still use the map from the previous page: a map centered on London, England.

Now we want to zoom when a user is clicking on the marker (We attach an event handler to a marker that zooms the map when clicked).

Here is the added code:


// Zoom to 9 when clicking on marker
google.maps.event.addListener(marker,'click',function() {

Try it yourself »

We register for event notifications using the addListener() event handler. That method takes an object, an event to listen for, and a function to call when the specified event occurs.

Pan Back to Marker

Here, we add an event handler to the map for changes to the 'center' property and pan the map back to the marker after 3 seconds on a center_changed event:


google.maps.event.addListener(map,'center_changed',function() {
  window.setTimeout(function() {

Try it yourself »

Open an InfoWindow When Clicking on The Marker

Click on the marker to show an infowindow with some text:


var infowindow = new google.maps.InfoWindow({
  content:"Hello World!"

google.maps.event.addListener(marker, 'click', function() {,marker);

Try it yourself »

Set Markers and Open InfoWindow for Each Marker

Run a function when the user clicks on the map.

The placeMarker() function places a marker where the user has clicked, and shows an infowindow with the latitudes and longitudes of the marker:


google.maps.event.addListener(map, 'click', function(event) {

function placeMarker(location) {
  var marker = new google.maps.Marker({
    position: location,
    map: map,
  var infowindow = new google.maps.InfoWindow({
    content: 'Latitude: ' + +
    '<br>Longitude: ' + location.lng()

Try it yourself »

Google Maps - Events Reference

Google Maps API Reference.


Color Picker