Home > Blockchain >  Google Street View Black Screen
Google Street View Black Screen

Time:12-26

I am trying to put a Google Streetview Panorama instance inside of a tab inside of an offcanvas element using Bootstrap 5.

If the default tab is the tab that contains the Streetview everything works fine as is demonstrated here:
https://codepen.io/taylormhicks90-the-bold/pen/OJxxqQN

If I use a different tab as the default tab the Streetview is initially black and doesn't work unless you make it fullscreen first as is demonstrated here:
https://codepen.io/taylormhicks90-the-bold/pen/Yzrrgro

While I have a workaround of using the streetview tab as the default tab this in not the desired functionality. I have spent a few hours digging through googles docs and trying different workarounds. Just hopping someone can help explain why this is happening and what I can do to get it functioning properly or at least point me in the right direction.

I even tried this where I load the streetview while the streetview tab is the default tab and then change the tab to the pictures tab after the streetview has loaded, but it doesn't work.
https://codepen.io/taylormhicks90-the-bold/pen/RwLLdeP

html:

<div class='container'>
  <div class='row'>
    <div class='col-12'>
      <h1>My Page</h1>
      <button class='btn btn-primary' data-bs-toggle="offcanvas" data-bs-target="#offcanvas" >Click Here For Off Canvas</button>
    </div>
  </div>
</div>
<!--Off Canvas-->
<div  tabindex="-1" id="offcanvas" data-bs-backdrop="false">
    <div >
        <ul  id="myTab" role="tablist">
            <li  role="presentation">
                <button  id="pictures-tab" data-bs-toggle="tab" data-bs-target="#pictures"
                        type="button" role="tab" aria-controls="pictures" aria-selected="true">Pictures
                </button>
            </li>
            <li  role="presentation">
                <button  id="streetview-tab" data-bs-toggle="tab" data-bs-target="#streetview"
                        type="button" role="tab" aria-controls="streetview" aria-selected="false">Street View
                </button>
            </li>
            <li  role="presentation">
                <button  id="map-tab" data-bs-toggle="tab" data-bs-target="#map" type="button"
                        role="tab" aria-controls="map" aria-selected="false">Map
                </button>
            </li>
        </ul>
        <button type="button"  data-bs-dismiss="offcanvas" aria-label="Close"></button>
    </div>
    <div >
        <div  id="myTabContent">
            <div  id="pictures" role="tabpanel" aria-labelledby="home-tab">
                <div >
                    <div class='col-12'>
                      <p> My Pictures Go Here </p>
                    </div>
                </div>
            </div>
            <div  id="streetview" role="tabpanel" aria-labelledby="profile-tab">
                <div id="streetviewContainer" ></div>
            </div>
            <div  id="map" role="tabpanel" aria-labelledby="contact-tab">
                <div id="mapContainer" ></div>
            </div>
        </div>
    </div>
</div>
<script
        src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAoIwp-hHU0Hv-UIK3RycqxgxKb_QZNV2E&callback=initMaps&v=weekly"
        async
></script>

Javascript:

    let map,streetView;
    function initMaps() {
        const location = { lat: 42.345573, lng: -71.098326 };
        map = new google.maps.Map(document.getElementById("mapContainer"), {
            center: location,
            zoom: 19,
        });
        new google.maps.Marker({
            position: location,
            map,
        });
        streetView = new google.maps.StreetViewPanorama(
            document.getElementById("streetviewContainer"),
            {
                position: location,
                zoom: 0,
            }
        )
    }

CodePudding user response:

Move StreetView call (new google.maps.StreetViewPanorama) into a function and then activate this function only when tab is active.

function loadStreetView() {
    streetView = new google.maps.StreetViewPanorama(
        document.getElementById("streetviewContainer"),
        {
            position: location,
            zoom: 0,
        }
    );
}


// listen to street view tab button only.
let streetviewTab = document.querySelector('#streetview-tab');
// listen to on shown.
streetviewTab.addEventListener('shown.bs.tab', function (event) {
  loadStreetView();
});

Only use this when default tab is not StreetView.

Reference: Bootstrap 5 tab events

  • Related