Embed Street View with the Google Maps API

E
Published in: Google Maps

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);
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Maps

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

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. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube