Click here to Skip to main content
Click here to Skip to main content

Google Maps StreetView

By , 15 Oct 2010
 
Sample Image

Introduction

Most Internet applications now use Google Maps. Google has provided a very rich API to use in our own applications. For the Google Maps, the latest feature is the "Panoroma view". Using it, I have created this Google Maps example. In this example, when the Google StreetView Man google Sreetview is dragged, the streets are highlighted wherever street view is enabled by Google. When it is dropped, the normal map is displayed. This way, the maps will be more little user friendly. This example uses JavaScript to call the Google Maps API.

Using the Code

In this part of the article, I will describe how to load Google Maps into an HTML page. Within the head section of an HTML document, include this script reference to the Google Maps API; the key should be changed if you need to upload this HTML in to a web server.

<script 
  src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=abcdefg&sensor=true_or_false" 
  type="text/javascript"></script>

Within the same HTML section, add the JavaScript function below. This function calls a Google map and displays it in a <div id="map_canvas"....

<script type="text/javascript">
  var marker = null;
  var myPano = null;


function initialize() {
  if (GBrowserIsCompatible()) {
       map = new GMap2(document.getElementById("map_canvas"));
       map.setCenter(new GLatLng(37.4419, -122.1419), 13);
       map.setUIToDefault();
  }
}
</script>

The HTML document should have <div id="map_canvas"... and from the body onload, the initialize() method should be called. Here is the full listing of code for loading a Google map into an HTML document.

<html>
<head>
<script 
  src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=abcdefg&sensor=true_or_false" 
  type="text/javascript"></script>
<script type="text/javascript">
    function initialize() {
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("map_canvas"));
            map.setCenter(new GLatLng(37.4419, -122.1419), 13);
            map.setUIToDefault();
          }
  }
</script>
</head>
<body onload="initialize()">
  <div id="map_canvas" style="width: 500px; height: 300px"></div>
</body>
</html>

From this point, I will describe how to add panorama view to the above Google map. To add panorama view, we need to add another <div id="stview"... to the HTML document, and the JavaScript should be change accordingly.

Adding a div tag into the HTML body section:

<div id="stview" style="width: 500px; height: 200px"></div>

The next step is to create a draggable marker on the map. The code for creating and adding a Google maker is listed here:

var latlang = map.getCenter(); 
var icon = new GIcon();

var imageUrl = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-0.png";

icon.image = imageUrl;
icon.iconSize = new GSize(49,52);
icon.iconAnchor =  new GPoint(25,36);
icon.infoWindowAnchor = new GPoint(25,6); 

marker = new GMarker(latlang, {"icon":icon, "draggable":true});
map.addOverlay(marker);

After adding a marker in to the map, the map should have listener events to listen when drag, drop events occur. To do this with Google maps, we should call the GEvent.addListener() method. The code for handling drag and drop events is listed below. The GStreetviewOverlay object should be defined before calling the addListener() event.

var streetOverlay = new GStreetviewOverlay();

GEvent.addListener(marker, "dragstart", function()
{
    map.addOverlay(streetOverlay);
}
); 
    
GEvent.addListener(marker, "dragend", function(overlay,latlang)
{            
    map.removeOverlay(streetOverlay);
    openWindow();
}
);

Finally, the panorama view should be opened. I wrote a separate method to do this. The openWindow() method is called when a drop event occurs.

function openWindow()
{
  myPano = new GStreetviewPanorama(document.getElementById("stview"));
  var latlang = marker.getLatLng();
  myPano.setLocationAndPOV(latlang);
}

Done. For complete code listings, please download the demo project.

Points of Interest

The variables marker and myPono should be global variables.

<script type="text/javascript">
var marker = null;
var myPano = null;

function initialize() {
    if (GBrowserIsCompatible()) {
        ................
        ................

History

  • 17th May, 2009: Initial post
  • 14th October, 2010: Updated demo

This is my first article on CodeProject.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rukshan Samanthilaka
Software Developer
Sri Lanka Sri Lanka
Member
I came to the the software industry in 2006 now i am familiar with C#, ASP.NET, SQL Server, JavaScript,Google Maps and Virtual Earth API and looking forward for a long way ahead.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
SuggestionClosest panomemberMember 855975211 Jan '12 - 23:23 
Hello, I like very much your script. But it's hard to open pano when you drag the "yellow man" over the map and the map zoom is less than 8. It can't find the nearest available pano and simply nothing happens!
QuestionHow do I attach the icon to the zoom slider?memberhneel13 Oct '10 - 11:15 
The code is working fine now. But I'd prefer the 'yellow man' icon to be attached to the slider, like on the regular Google Maps page. Now it's fixed on a position on the map, and when I zoom in to another part of the map, the icon goes outof sight.
AnswerRe: How do I attach the icon to the zoom slider?memberRukshan Samanthilaka14 Oct '10 - 7:31 
Hi hneel,
 
One approach is to set map center according to current marker point. u can do it by adding
 
map.setCenter(marker.getLatLng(), 13);
 
to "dragend" event listener.
 

//change ur code as this
 
GEvent.addListener(marker, "dragend", function(overlay,latlang)
{
map.removeOverlay(streetOverlay);
openWindow();
map.setCenter(marker.getLatLng(), 13); // additional line.
}
 
cheers
Rukshan
SHAN

GeneralIcon URL needs to be updatedmemberhneel13 Oct '10 - 10:24 
It has changed to http://maps.gstatic.com/mapfiles/cb/man_arrow-0.png[^]
GeneralRe: Icon URL needs to be updatedmemberRukshan Samanthilaka14 Oct '10 - 7:22 
Hi hneel,
 
Thanks for the update. this really should be updated, already sent my changed sample demo to "the code project".
 
Thank u once again..
 
cheers
Rukshan
SHAN

GeneralA thank you from a newbie looking for help...memberSorrytoask30 Mar '10 - 4:19 
Just wanted to say a thanks for this helped me to understand more about gogggle maps and street view. Being new to this I wondered with your code is it possible to have the streetview show with the map onload of page at the moment I can only get it to show map then when I move the icon the streetview appears.I have tried adding this but to no avail any help for a newbie be appreciated
function Initialize() {
InitializeMap();
InitializeStreetView();
}
GeneralJQuery Google Maps PluginmemberLes Green24 Dec '09 - 8:42 
I created a JQuery Plugin that incorporates the StreetViewPanorama and Street View Overlay.
Read my post, imGoogleMaps 0.9 - Multiple Addreses, Street View
 
Les Green
http://grasshoppperpebbles.com
Generalgood to see another google st view program [modified]memberkalyan_mpl11 Jul '09 - 17:38 
i have also developed something similar to this. on first glance i was stunned to see that u have managed to use sprites. but after goin through ur code it seems that the marker is a static one i.e fixed at 0 degree pov. Smile | :) If ur interested i can send u my code its got 2d as well as 3d maps ,streetview with dynamic marker showing pov direction, poi search etc hey i have also recently integrated virtual earth and wikimapia maps into my app. by the way my app has c# as frontend and backend is in javascript.Cool | :cool: Big Grin | :-D
 
modified on Saturday, October 10, 2009 5:49 AM

GeneralRe: good to see another google st view programmemberguido5322 Oct '09 - 2:55 
Hello,
 
I would be very interested in your code.
 
I am trying to move through street view using a program rather than a human with his mouse.
 
rgds
Guido Bugmann
Plymouth
GeneralRe: good to see another google st view programmemberbrunoroda12 Jan '12 - 0:18 
Hello,
i am having the same goal.
I want to use my own keyboard instead the mouse or arrow keys from the keyboard.
Did you found something that can help me please?
 
thank you in advance.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 15 Oct 2010
Article Copyright 2009 by Rukshan Samanthilaka
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid