Street View Events
Stay organized with collections
Save and categorize content based on your preferences.
Page Summary
-
This example demonstrates tracking Street View events to monitor the performance of the
StreetViewPanoramaobject. -
The sample code displays a Street View panorama alongside a panel that updates with real-time data like position, POV, and pano ID.
-
Users can interact with the panorama, and the associated data will be reflected in the panel.
-
The example utilizes event listeners like
pano_changed,links_changed,position_changed, andpov_changedto capture and display relevant data.
This example displays a Street View panorama, alongside a window that tracks
several types of data associated with Street View events. This data can be
tracked to better understand the performance of the underlying
StreetViewPanorama
object.
Read the documentation.
TypeScript
functioninitPano(){ constpanorama=newgoogle.maps.StreetViewPanorama( document.getElementById("pano")asHTMLElement, { position:{lat:37.869,lng:-122.255}, pov:{ heading:270, pitch:0, }, visible:true, } ); panorama.addListener("pano_changed",()=>{ constpanoCell=document.getElementById("pano-cell")asHTMLElement; panoCell.innerHTML=panorama.getPano(); }); panorama.addListener("links_changed",()=>{ constlinksTable=document.getElementById("links_table")asHTMLElement; while(linksTable.hasChildNodes()){ linksTable.removeChild(linksTable.lastChildasChildNode); } constlinks=panorama.getLinks(); for(constiinlinks){ constrow=document.createElement("tr"); linksTable.appendChild(row); constlabelCell=document.createElement("td"); labelCell.innerHTML="<b>Link: "+i+"</b>"; constvalueCell=document.createElement("td"); valueCell.innerHTML=links[i].descriptionasstring; linksTable.appendChild(labelCell); linksTable.appendChild(valueCell); } }); panorama.addListener("position_changed",()=>{ constpositionCell=document.getElementById( "position-cell" )asHTMLElement; (positionCell.firstChildasHTMLElement).nodeValue= panorama.getPosition()+""; }); panorama.addListener("pov_changed",()=>{ constheadingCell=document.getElementById("heading-cell")asHTMLElement; constpitchCell=document.getElementById("pitch-cell")asHTMLElement; (headingCell.firstChildasHTMLElement).nodeValue= panorama.getPov().heading+""; (pitchCell.firstChildasHTMLElement).nodeValue= panorama.getPov().pitch+""; }); } declareglobal{ interfaceWindow{ initPano:()=>void; } } window.initPano=initPano;
JavaScript
functioninitPano(){ constpanorama=newgoogle.maps.StreetViewPanorama( document.getElementById("pano"), { position:{lat:37.869,lng:-122.255}, pov:{ heading:270, pitch:0, }, visible:true, }, ); panorama.addListener("pano_changed",()=>{ constpanoCell=document.getElementById("pano-cell"); panoCell.innerHTML=panorama.getPano(); }); panorama.addListener("links_changed",()=>{ constlinksTable=document.getElementById("links_table"); while(linksTable.hasChildNodes()){ linksTable.removeChild(linksTable.lastChild); } constlinks=panorama.getLinks(); for(constiinlinks){ constrow=document.createElement("tr"); linksTable.appendChild(row); constlabelCell=document.createElement("td"); labelCell.innerHTML="<b>Link: "+i+"</b>"; constvalueCell=document.createElement("td"); valueCell.innerHTML=links[i].description; linksTable.appendChild(labelCell); linksTable.appendChild(valueCell); } }); panorama.addListener("position_changed",()=>{ constpositionCell=document.getElementById("position-cell"); positionCell.firstChild.nodeValue=panorama.getPosition()+""; }); panorama.addListener("pov_changed",()=>{ constheadingCell=document.getElementById("heading-cell"); constpitchCell=document.getElementById("pitch-cell"); headingCell.firstChild.nodeValue=panorama.getPov().heading+""; pitchCell.firstChild.nodeValue=panorama.getPov().pitch+""; }); } window.initPano=initPano;
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; } #floating-panel{ position:absolute; top:10px; left:25%; z-index:5; background-color:#fff; padding:5px; border:1pxsolid#999; text-align:center; font-family:"Roboto","sans-serif"; line-height:30px; padding-left:10px; } #pano{ width:50%; height:100%; float:left; } #floating-panel{ width:45%; height:100%; float:right; text-align:left; overflow:auto; position:static; border:0pxsolid#999; }
HTML
<html> <head> <title>Street View Events</title> <link rel="stylesheet" type="text/css" href="./style.css" /> <script type="module" src="./index.js"></script> </head> <body> <div id="pano"></div> <div id="floating-panel"> <table> <tr> <td><b>Position</b></td> <td id="position-cell"> </td> </tr> <tr> <td><b>POV Heading</b></td> <td id="heading-cell">270</td> </tr> <tr> <td><b>POV Pitch</b></td> <td id="pitch-cell">0.0</td> </tr> <tr> <td><b>Pano ID</b></td> <td id="pano-cell"> </td> </tr> <table id="links_table"></table> </table> </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=initPano&v=weekly" defer ></script> </body> </html>
Clone Sample
Git and Node.js are required to run this sample locally. Follow these instructions to install Node.js and NPM. The following commands clone, install dependencies and start the sample application.
gitclone-bsample-streetview-eventshttps://github.com/googlemaps/js-samples.gitcdjs-samplesnpminpmstart
Other samples can be tried by switching to any branch beginning with sample-SAMPLE_NAME.
gitcheckoutsample-SAMPLE_NAMEnpminpmstart