Overlays Within Street View
Stay organized with collections
Save and categorize content based on your preferences.
Page Summary
-
This example demonstrates displaying markers for various points of interest around Astor Place, New York City, on a Google Map.
-
It features a toggle button that switches to Street View, where the same markers are overlaid on the panoramic imagery.
-
The code utilizes the Google Maps JavaScript API to create the map, markers, and Street View panorama, along with event listeners for user interaction.
-
The sample code is provided in both JavaScript and TypeScript, with instructions for running it locally using Git and Node.js or trying it online via JSFiddle and Google Cloud Shell.
This example creates a map that displays markers to denote various locations around Astor Place, New York City. It includes a toggle to switch to Street View, where the same markers are visible.
Read the documentation.
TypeScript
letpanorama:google.maps.StreetViewPanorama; letinnerMap:google.maps.Map; asyncfunctioninit(){ // Request needed libraries. const[{Marker}]=awaitPromise.all([ google.maps.importLibrary('marker'), google.maps.importLibrary('maps'), ]); // Set the location of Astor Place. constastorPlace={lat:40.729884,lng:-73.990988}; constmapElement=document.querySelector('gmp-map')!; innerMap=mapElement.innerMap; document .getElementById('streetview-toggle-button')! .addEventListener('click',toggleStreetView); constcafeIcon=document.createElement('img'); cafeIcon.src=newURL('./public/cafe_icon.svg',import.meta.url).href; constdollarIcon=document.createElement('img'); dollarIcon.src=newURL('./public/bank_icon.svg',import.meta.url).href; constbusIcon=document.createElement('img'); busIcon.src=newURL('./public/bus_icon.svg',import.meta.url).href; // Set up the markers on the map newMarker({ position:{lat:40.730031,lng:-73.991428}, map:innerMap, title:'Cafe', icon:cafeIcon.src, }); newMarker({ position:{lat:40.729681,lng:-73.991138}, map:innerMap, title:'Bank', icon:dollarIcon.src, }); newMarker({ position:{lat:40.729559,lng:-73.990741}, map:innerMap, title:'Bus Stop', icon:busIcon.src, }); // We get the map's default panorama and set up some defaults. // Note that we don't yet set it visible. panorama=innerMap.getStreetView()!;// TODO fix type panorama.setPosition(astorPlace); panorama.setPov({ heading:265, pitch:0, }); } functiontoggleStreetView():void{ consttoggle=panorama.getVisible(); if(!toggle){ panorama.setVisible(true); }else{ panorama.setVisible(false); } } voidinit();
JavaScript
letpanorama; letinnerMap; asyncfunctioninit(){ // Request needed libraries. const[{Marker}]=awaitPromise.all([ google.maps.importLibrary('marker'), google.maps.importLibrary('maps'), ]); // Set the location of Astor Place. constastorPlace={lat:40.729884,lng:-73.990988}; constmapElement=document.querySelector('gmp-map'); innerMap=mapElement.innerMap; document .getElementById('streetview-toggle-button') .addEventListener('click',toggleStreetView); constcafeIcon=document.createElement('img'); cafeIcon.src=newURL('./public/cafe_icon.svg',import.meta.url).href; constdollarIcon=document.createElement('img'); dollarIcon.src=newURL('./public/bank_icon.svg',import.meta.url).href; constbusIcon=document.createElement('img'); busIcon.src=newURL('./public/bus_icon.svg',import.meta.url).href; // Set up the markers on the map newMarker({ position:{lat:40.730031,lng:-73.991428}, map:innerMap, title:'Cafe', icon:cafeIcon.src, }); newMarker({ position:{lat:40.729681,lng:-73.991138}, map:innerMap, title:'Bank', icon:dollarIcon.src, }); newMarker({ position:{lat:40.729559,lng:-73.990741}, map:innerMap, title:'Bus Stop', icon:busIcon.src, }); // We get the map's default panorama and set up some defaults. // Note that we don't yet set it visible. panorama=innerMap.getStreetView();// TODO fix type panorama.setPosition(astorPlace); panorama.setPov({ heading:265, pitch:0, }); } functiontoggleStreetView(){ consttoggle=panorama.getVisible(); if(!toggle){ panorama.setVisible(true); }else{ panorama.setVisible(false); } } voidinit(); export{};
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; } #streetview-toggle-button{ height:40px; display:flex; align-items:center; justify-content:center; padding:017px; border:none; background:white; cursor:pointer; border-radius:2px; box-shadow:01px4px-1pxrgba(0,0,0,0.3); margin:10px0px10px-2px; font-family:Roboto,Arial,sans-serif; font-size:18px; font-weight:400; color:rgb(86,86,86); } #streetview-toggle-button:hover{ background:#f4f4f4; color:#000; }
HTML
<html>
<head>
<title>Overlays Within Street View</title>
<link rel="stylesheet" type="text/css" href="./style.css" />
<script type="module" src="./index.js"></script>
<script>
// prettier-ignore
(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
key: "GOOGLE_MAPS_API_KEY"
});
</script>
</head>
<body>
<gmp-map map-id="DEMO_MAP_ID" center="40.729884, -73.990988" zoom="18">
<input
type="button"
value="Toggle Street View"
id="streetview-toggle-button"
slot="control-block-start-inline-start" />
</gmp-map>
</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.
gitclonehttps://github.com/googlemaps-samples/js-api-samples.gitcdsamples/streetview-overlaysnpminpmstart