Embed Street View with the Google Maps API

The Embed Google Maps app uses the Google Maps API (v3) to generate the embed code for placing street view and satellite images in your website.

When you open the app, it centers the Google Map around your current geolocation. As you zoom or pan the map, the Street View location and Point-of-View, specifically zoom, pitch and heading, are appended to the IFRAME url.

Heading defines the rotation angle around the camera locus while pitch defines the angle variance “up” or “down” from the camera’s initial default pitch.

var map,
  pano,
  lat = 40.7045737,
  lng = -74.0089673;

function generateURL(parameters) {
  var url = 'http://my.ctrlq.org/maps/#' + parameters,
    iframe =
      '<iframe width="600" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="URL"></iframe>';

  document.getElementById('embed-code').value = iframe.replace('URL', url);
  document.getElementById('link').href = url;
}

function updateSV() {
  var parameters,
    pov = pano.getPov(),
    pos = pano.getPosition();

  if (pos) {
    lat = pos.lat();
    lng = pos.lng();
  }

  parameters = 'street|' + pov.zoom + '|' + pov.heading + '|' + pov.pitch + '|' + lat + '|' + lng;

  generateURL(parameters);
}

function updateMap() {
  var center = map.getCenter();
  generateURL(map.getMapTypeId() + '|' + map.getZoom() + '|' + center.lat() + '|' + center.lng());
}

function initialize() {
  var input,
    autocomplete,
    mapOptions = {
      zoom: 16,
      center: new google.maps.LatLng(lat, lng),
      streetViewControl: true,
      panControl: true,
      scaleControl: true,
      mapTypeId: 'roadmap',
    };

  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  input = document.getElementById('pac-input');
  map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

  google.maps.event.addListener(map, 'maptypeid_changed', updateMap);
  google.maps.event.addListener(map, 'idle', updateMap);

  pano = map.getStreetView();

  google.maps.event.addListener(pano, 'visible_changed', updateSV);
  google.maps.event.addListener(pano, 'position_changed', updateSV);
  google.maps.event.addListener(pano, 'links_changed', updateSV);
  google.maps.event.addListener(pano, 'pano_changed', updateSV);
  google.maps.event.addListener(pano, 'pov_changed', updateSV);

  autocomplete = new google.maps.places.Autocomplete(input);
  autocomplete.bindTo('bounds', map);

  google.maps.event.addListener(autocomplete, 'place_changed', function () {
    var place = autocomplete.getPlace();

    if (!place.geometry) {
      return;
    }

    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
      map.setZoom(17);
    }
  });

  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (position) {
      map.setCenter(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
    });
  }
}

google.maps.event.addDomListener(window, 'load', initialize);

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻