14500+ website owners are using this wordpress map plugin

Buy Now - $89

How to Use Simple Click Events in Maps?

Introduction:

This tutorial shows how to use event listeners with simple click events.

  • When a marker is clicked, this function listens for the click event and zooms the map.
  • After 3 seconds, it listens for the center changed event and pans the map back to the marker.

User events (such as mouse clicks) are sent from the DOM to the Maps JavaScript API. These events are unique from the usual DOM events.

Code:

function initMap(): void {
  const myLatlng = { lat: -25.363, lng: 131.044 };

  const map = new google.maps.Map(
    document.getElementById("map") as HTMLElement,
    {
      zoom: 4,
      center: myLatlng,
    }
  );

  const marker = new google.maps.Marker({
    position: myLatlng,
    map,
    title: "Click to zoom",
  });

  map.addListener("center_changed", () => {
    // 3 seconds after the center of the map has changed, pan back to the
    // marker.
    window.setTimeout(() => {
      map.panTo(marker.getPosition() as google.maps.LatLng);
    }, 3000);
  });

  marker.addListener("click", () => {
    map.setZoom(8);
    map.setCenter(marker.getPosition() as google.maps.LatLng);
  });
}

declare global {
  interface Window {
    initMap: () => void;
  }
}
window.initMap = initMap;
export {};

Explanation:

  • The function initMap() is created and it consists of the map properties.
  • The ‘centre’ attribute defines where the map should be centered with the help of the latitude and longitude coordinates.
  • const myLatlng = { lat: -25.363, lng: 131.044 };
    center: myLatlng,
    
  • The ‘zoom’ attribute specifies the map’s zoom level.
  • zoom: 4,
    
  • The line, “map = new google.maps.Map(document.getElementById(“map”)”; creates a new map inside the <div> element with the help of the mentioned id which is ‘map’ as an HTMLElement.
  • const map = new google.maps.Map(
        document.getElementById("map") as HTMLElement,
    
  • The markers are added to the map using the Marker(). Positions are mentioned for the marker on the map.
  • const marker = new google.maps.Marker({
        position: myLatlng,
        map,
        title: "Click to zoom",
      });
    
  • Further, the addListener() is added to the map for ‘center_changed’ and ‘click’, mentioning the parameters.
  • map.addListener("center_changed", () => {
        // 3 seconds after the center of the map has changed, pan back to the
        // marker.
        window.setTimeout(() => {
          map.panTo(marker.getPosition() as google.maps.LatLng);
        }, 3000);
      });
    
    marker.addListener("click", () => {
        map.setZoom(8);
        map.setCenter(marker.getPosition() as google.maps.LatLng);
      });
    }
    
  • Lastly, the function is called and executed.
  • window.initMap = initMap;
    

Conclusion:

Thus, in this tutorial we learned how to use the event listeners.

Live Example

Install Plugin Now!

This plugin is exclusively available at codecanyon.net. You'll get free updates and full support to use this plugin.