JavaScript: Show Street View Map without Clicking Show button

Viewed 130

Good day. I have this code that show street map base on the Lat and Longitude.

Button Click

<a id="street-view" class="btn btn-outline-primary btn-lg m-1 px-3" href="javascript:;">
                                <i class="fa fa-map small mr-2"></i>
                                View Neighbourhood
                            </a>

This ID display the Street View Map

<div id="street"></div>

CSS Code

#map,
#street {
    display: none;
    height: 450px;
    width: 100%;
}

Js Code that display Map

// Initialize and add street view map
function initStreetView() {
    // The location to load the street view at
    const gpFarms = { lat: 4.836824, lng: 7.0115864 };
    const streetDiv = document.getElementById("street");
    const panorama = new google.maps.StreetViewPanorama(streetDiv, {
        position: gpFarms,
    });
}

I Want the street view Map to Display without Clicking the button

1 Answers

You would only need to remove the button and execute the panorama on page load. Here is a JSFiddle sample for your reference: https://jsfiddle.net/pintsik999/z81wx2ku/2/

You can also just see the code below:

function initialize() {
  const gpFarms = {
    lat: 4.836824,
    lng: 7.0115864
  };
  const streetDiv = document.getElementById("street-view");
  const panorama = new google.maps.StreetViewPanorama(
    streetDiv, {
      position: gpFarms
    }
  );
}
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

#street-view {
  height: 100%;
}
<div id="street-view"></div>

<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?&callback=initialize&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk" async></script>

Related