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

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

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

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

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

We will never send any spam emails. Promise.