POI Click Events Stay organized with collections Save and categorize content based on your preferences.
Page Summary
This example showcases how to use click event listeners on Points of Interest (POIs) on a Google Map.
When a POI icon is clicked, the code calculates and displays a route from a predefined origin to the clicked POI using the Directions Service.
It also retrieves and displays detailed information about the clicked POI, such as name, address, and place ID, in an info window.
The example prevents the default info window from showing and uses
event.stop()to manage click behavior.
This example demonstrates the use of click event listeners on POIs (points ofinterest). It listens for theclick event on a POI icon and thenuses theplaceId from the event data with adirectionsService.route request to calculate and display a route tothe clicked place. It also uses theplaceId to get more details ofthe place.
Read thedocumentation.
TypeScript
functioninitMap():void{constorigin={lat:-33.871,lng:151.197};constmap=newgoogle.maps.Map(document.getElementById("map")asHTMLElement,{zoom:18,center:origin,});newClickEventHandler(map,origin);}functionisIconMouseEvent(e:google.maps.MapMouseEvent|google.maps.IconMouseEvent):eisgoogle.maps.IconMouseEvent{return"placeId"ine;}classClickEventHandler{origin:google.maps.LatLngLiteral;map:google.maps.Map;directionsService:google.maps.DirectionsService;directionsRenderer:google.maps.DirectionsRenderer;placesService:google.maps.places.PlacesService;infowindow:google.maps.InfoWindow;infowindowContent:HTMLElement;constructor(map:google.maps.Map,origin:google.maps.LatLngLiteral){this.origin=origin;this.map=map;this.directionsService=newgoogle.maps.DirectionsService();this.directionsRenderer=newgoogle.maps.DirectionsRenderer();this.directionsRenderer.setMap(map);this.placesService=newgoogle.maps.places.PlacesService(map);this.infowindow=newgoogle.maps.InfoWindow();this.infowindowContent=document.getElementById("infowindow-content")asHTMLElement;this.infowindow.setContent(this.infowindowContent);// Listen for clicks on the map.this.map.addListener("click",this.handleClick.bind(this));}handleClick(event:google.maps.MapMouseEvent|google.maps.IconMouseEvent){console.log("You clicked on: "+event.latLng);// If the event has a placeId, use it.if(isIconMouseEvent(event)){console.log("You clicked on place:"+event.placeId);// Calling e.stop() on the event prevents the default info window from// showing.// If you call stop here when there is no placeId you will prevent some// other map click event handlers from receiving the event.event.stop();if(event.placeId){this.calculateAndDisplayRoute(event.placeId);this.getPlaceInformation(event.placeId);}}}calculateAndDisplayRoute(placeId:string){constme=this;this.directionsService.route({origin:this.origin,destination:{placeId:placeId},travelMode:google.maps.TravelMode.WALKING,}).then((response)=>{me.directionsRenderer.setDirections(response);}).catch((e)=>window.alert("Directions request failed due to "+status));}getPlaceInformation(placeId:string){constme=this;this.placesService.getDetails({placeId:placeId},(place:google.maps.places.PlaceResult|null,status:google.maps.places.PlacesServiceStatus)=>{if(status==="OK"&&place&&place.geometry&&place.geometry.location){me.infowindow.close();me.infowindow.setPosition(place.geometry.location);(me.infowindowContent.children["place-icon"]asHTMLImageElement).src=place.iconasstring;(me.infowindowContent.children["place-name"]asHTMLElement).textContent=place.name!;(me.infowindowContent.children["place-id"]asHTMLElement).textContent=place.place_idasstring;(me.infowindowContent.children["place-address"]asHTMLElement).textContent=place.formatted_addressasstring;me.infowindow.open(me.map);}});}}declareglobal{interfaceWindow{initMap:()=>void;}}window.initMap=initMap;
JavaScript
functioninitMap(){constorigin={lat:-33.871,lng:151.197};constmap=newgoogle.maps.Map(document.getElementById("map"),{zoom:18,center:origin,});newClickEventHandler(map,origin);}functionisIconMouseEvent(e){return"placeId"ine;}classClickEventHandler{origin;map;directionsService;directionsRenderer;placesService;infowindow;infowindowContent;constructor(map,origin){this.origin=origin;this.map=map;this.directionsService=newgoogle.maps.DirectionsService();this.directionsRenderer=newgoogle.maps.DirectionsRenderer();this.directionsRenderer.setMap(map);this.placesService=newgoogle.maps.places.PlacesService(map);this.infowindow=newgoogle.maps.InfoWindow();this.infowindowContent=document.getElementById("infowindow-content");this.infowindow.setContent(this.infowindowContent);// Listen for clicks on the map.this.map.addListener("click",this.handleClick.bind(this));}handleClick(event){console.log("You clicked on: "+event.latLng);// If the event has a placeId, use it.if(isIconMouseEvent(event)){console.log("You clicked on place:"+event.placeId);// Calling e.stop() on the event prevents the default info window from// showing.// If you call stop here when there is no placeId you will prevent some// other map click event handlers from receiving the event.event.stop();if(event.placeId){this.calculateAndDisplayRoute(event.placeId);this.getPlaceInformation(event.placeId);}}}calculateAndDisplayRoute(placeId){constme=this;this.directionsService.route({origin:this.origin,destination:{placeId:placeId},travelMode:google.maps.TravelMode.WALKING,}).then((response)=>{me.directionsRenderer.setDirections(response);}).catch((e)=>window.alert("Directions request failed due to "+status));}getPlaceInformation(placeId){constme=this;this.placesService.getDetails({placeId:placeId},(place,status)=>{if(status==="OK"&&place&&place.geometry&&place.geometry.location){me.infowindow.close();me.infowindow.setPosition(place.geometry.location);me.infowindowContent.children["place-icon"].src=place.icon;me.infowindowContent.children["place-name"].textContent=place.name;me.infowindowContent.children["place-id"].textContent=place.place_id;me.infowindowContent.children["place-address"].textContent=place.formatted_address;me.infowindow.open(me.map);}});}}window.initMap=initMap;
CSS
/* Always set the map height explicitly to define the size of the div * element that contains the map. */#map{height:100%;}/* Optional: Makes the sample page fill the window. */html,body{height:100%;margin:0;padding:0;}.title{font-weight:bold;}#infowindow-content{display:none;}#map#infowindow-content{display:inline;}
HTML
<html> <head> <title>POI Click Events</title> <link rel="stylesheet" type="text/css" href="./style.css" /> <script type="module" src="./index.js"></script> </head> <body> <div></div> <div> <img src="" height="16" width="16" /> <span></span><br /> Place ID <span></span><br /> <span></span> </div> <!-- The `defer` attribute causes the script to execute after the full HTML document has been parsed. For non-blocking uses, avoiding race conditions, and consistent behavior across browsers, consider loading using Promises. See https://developers.google.com/maps/documentation/javascript/load-maps-js-api for more information. --> <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&libraries=places&v=weekly" defer ></script> </body></html>
Try Sample
Clone Sample
Git and Node.js are required to run this sample locally. Follow theseinstructions to install Node.js and NPM. The following commands clone, install dependencies and start the sample application.
gitclone-bsample-event-poihttps://github.com/googlemaps/js-samples.gitcdjs-samplesnpminpmstart
Other samples can be tried by switching to any branch beginning withsample-SAMPLE_NAME.
gitcheckoutsample-SAMPLE_NAMEnpminpmstart
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-18 UTC.