Web Development Blog

Twitter

27 Apr, 2009

Code to embed Google Map and Street View

Web Development » Google, Snippets, Tutorials » Code to embed Google Map and Street View

You will need to sign up for the Google Maps API key and replace “INSERT_API_KEY_HERE” with your API key.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Google Map and Street View</title>

<script src="http://maps.google.com/maps?file=api&v=2&key=INSERT_API_KEY_HERE" type="text/javascript"></script>

<script type="text/javascript">
    //<![CDATA[

    function load() {
      if (GBrowserIsCompatible()) {
		var lat = -33.867141;
		var lng = 151.207114;

        var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(lat,lng), 15);

		var infoTabs = [
  		new GInfoWindowTab("Address", "Sydney<br /> NSW, Australia"),
		];

		// Place a marker in the center of the map and open the info window
		var marker = new GMarker(map.getCenter());
		GEvent.addListener(marker, "click", function() {
  		marker.openInfoWindowTabsHtml(infoTabs);
		});
		map.addOverlay(marker);
		marker.openInfoWindowTabsHtml(infoTabs);

		var point = new GLatLng(lat,lng);
		panoramaOptions = { latlng:point };
		pano = new GStreetviewPanorama(document.getElementById("streetview"), panoramaOptions);
		GEvent.addListener(pano);

	}
    }

    //]]>
</script>	

</head>
<body onload="load()" onunload="GUnload()">

<h3>Google Map</h3>
<div id="map" style="width: 500px; height: 400px"></div>
<h3>Street View</h3>
<div id="streetview" style="width: 500px; height: 400px"></div>

</body>
</html>

Books on Google Maps

Other similiar posts that you might be interested in:

18 Responses to "Code to embed Google Map and Street View"

1 | fl3x7

July 15th, 2009 at 1:04 am

Avatar

this is awesome. Thank you so much!! Exactly what i needed.

Would it be possible to import the lat and long values from an sql database?

This is my first time using the google api and i only know basic php so any help would be really appreciated :D

again thanks for the code :D

fl3×7

2 | eisabai

July 15th, 2009 at 11:39 am

Avatar

Yes, you can get the values from the sql database and assign the values to javascript variables dynamically. For example, if you are using php:

<?php
// get lat and lng values from the database and assign them to $lat and $lng
?>



function load() {
if (GBrowserIsCompatible()) {
var lat = <?php echo $lat;?>
var lng = <?php echo $lng;?>

3 | fl3x7

July 15th, 2009 at 6:09 pm

Avatar

Thank you for your quick response!

This is amazing. i will try this tonight. Hope it works.

Again thank you so much!!! :)

4 | fl3x7

July 16th, 2009 at 6:22 am

Avatar

Hello again :D

i tried the echo thing but for some reason nothing shows up, no google map or street view :(

var geocoder = null;
function load() {
if (GBrowserIsCompatible()) {
var lat =
var lng =

The variable scope reaches there i think as i echo just above the script and its fine.

Not sure what to do now :( … really like this version of map and street view as you only have to put long and lat in once as well.

hope you can help. If you need more please info please let me know. It would be even more amazing if i could email you my code :)

My email address is what ive submitted with this post :)

thank you for your help, greatly appreciated :D

5 | eisabai

July 16th, 2009 at 10:31 am

Avatar

Hi fl3×7,
You can email me at eisabainyo at yahoo.com and I will have a look at your code for you.

6 | fl3x7

July 17th, 2009 at 4:50 am

Avatar

in case any one is having trouble getting the php to echo proeprly try adding () are round each echo. eg:

var lat =() instead of just:
var lat =

hope this helps someone :)

fl3×7

7 | Ariel

August 25th, 2009 at 4:19 pm

Avatar

there’s something problem when i change the lat and long it will never run. Anyone can help.. tnx

8 | Swathi

September 23rd, 2009 at 5:52 pm

Avatar

I have embedded the Google Map in my site but I dont know how to locate my Factory’s Location.Can you please help me in including the address of my Factory.

9 | eisabai

September 24th, 2009 at 10:21 am

Avatar

You can get geocode for your address via this URL.

http://maps.google.com/maps/geo?key=API_KEY_HERE&q=YOUR_ADDRESS_HERE&output=xml

View the source of the page and look for <coordinates> tag.

10 | grant

November 6th, 2009 at 7:58 am

Avatar

Thanks for your sharing of good tips.
If I click the marker, there is script error and no street view. I am testing it with asp.net. Is there anything I need to make sure?
Thanks.

11 | Neerav

November 11th, 2009 at 1:58 am

Avatar

hello Thankx for sharing this awesome stuff man….cheers..!!!
nd ya i want to find a place by giving postal code nd place around it

12 | Rafi Akbar

November 24th, 2009 at 9:17 pm

Avatar

I have always used Google maps and I would like to know how to improve the directions on the layout, I am looking for a buddy for improve my skills. Street view is cool and I want to add this with a scallable size image too

Do email me back

rafiul_akbar@hotmail.com

13 | James

December 7th, 2009 at 10:22 pm

Avatar

Hi,

This is a very good stuff. Just wondering how I can change/replace the lat and long with a location/street address from a database.

14 | eisabai

December 8th, 2009 at 10:57 am

Avatar

If you have street addresses but no geocodes in your database table, the ideal solution is to add new columns to your table for lat, long values. There are quite a few online tools that let you geocode addresses in batch. Google for “batch geocoder” or similar.

15 | James

December 8th, 2009 at 7:29 pm

Avatar

Thanks for the reply.

Just to ensure that I clearly understood ur explanation.

I have created a field called lat and long in my database. My problem now is “how do i get the data in the address field to populate the ‘geocode systems’ and result from geocode to populate the lat and long field in database, so that the above scrips can use them?

I have a feeling i javascript can do this but have no idea how to go about it.

Will appreciate comment if i’m on the right direction and how to moveforward or if there is a better way to do this.

Many thanks.

James.

16 | anil singh

July 17th, 2010 at 12:34 am

Avatar

Hi I am a school principal and have keen interest java programming and google maps.

I’ve been using this code and have been unable to change the image in the sample code you provided.

i have a search and submit button, i would like to have users req an area and see the google street view and map as in your sample code.

i have read about flash and have been looking at similar sites

17 | anil singh

July 17th, 2010 at 12:39 am

Avatar

I’ve used your sample code on a new html page and it shows the sample image maps.

However on my website http://www.durban24.com it does not show? I have spent many hours and have not been unsuccessful.

When I did get the maps to show I could not change them.

Can you look at my site and advise, Please.

I am a school principal and am learning basic java programming

18 | Luke

August 18th, 2010 at 10:58 pm

Avatar

I set this up to work with a url variable for long.lat and it works fine but how can i set the direction and angle?

Comment Form

ebook
Subscribe to our newsletter and receive FREE e-book "7 Days Exercise to Build More Traffic To Your Blog"
Your name:  
Your email:  

  • About
    The blog Web Development Blog is where I keep myself up to date with the latest technologies in the industry and share my ideas and thoug...
  • Adsense Tips and Tricks
    With personal and commercial blogs, community websites, and social pages becoming the hot topic of the Internet in the last few years, many...
  • Advertise With Us
    Web Development Blog is a blog about Web Development, Web Design, Web Applications, Web 2.0, AJAX, Search Engine Optimisation, Latest Techno...
  • Archive
    Grab yourself a cup of coffee or tea, sit back and browse through an archive of all the blog posts on Web Development Blog....
  • Choosing a Content Management System
    The most commonly asked question when it comes to Content Management System is whether to build or buy (pre-built). While there are many fa...
  • Coupons
    Use the following coupon codes, promos and discount codes to save money on web hosting, domain names, website templates, ebooks, software, p...
  • Customised WordPress Themes
    If you would like a customised WordPress theme for your blog or your website, read on: (or head to WordPress Themes page to get free WordPre...
  • Favourite Poems
    If by Rudyard Kipling If you can keep your head when all about you Are losing theirs and blaming it on you, If you can trust yourself w...
  • Favourites
    Books Web Development Books Magazines Glamour Reader's Digest Software Adobe Photoshop CS4 Trend Micro Antivirus + Antispyw...
  • How to Start a Blog?
    What's a blog? According to Wikipedia, a blog is a user-generated website where entries are made in journal style and displayed in a reve...
  • Introduction to JSP Standard Tag Library (JSTL Basics)
    Tag libraries to include in your .jsp page <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> &l...
  • Links
    Free Anonymous Proxy Increases your privacy and security on the Internet by using our free, fast and easy to use web proxy. data backu...
  • Making a Passive Income Online
    Passive income, in my opinion, means an income which does not require a lot of regular maintenance work or continuing effort. I have been t...
  • Online Distribution Channels
    One of the importances of any website or business is to bring your products or services to the right people and to reach the target audience...
  • Recent Projects
    Followings are some of the recent projects I have done in 2006 -- 2007. Check out my latest business venture Web design Sydney to get your...
  • Recommended Web Development Books
    Many developers often ask me what books I read and what books I recommend in regards to web development and web technologies such as AJAX, J...
  • Search
    Looking for something on Web Developement Blog? Use this search tool powered by Google Custom Search to find what you are looking for just ...
  • Seven Tips to Building an Online Presence for your Business
    1. Get a domain name for your business The first and foremost step in building an online presence is to secure a domain name for your busi...
  • Seven Tips to Increasing Your Website’s Traffic Using SEO
    SEO is the buzz word - many people have mentioned it and many have heard of it too, but very few people know how to implement it properly. ...
  • Subscribe
    Newsletter [newsletter] RSS 2.0 feeds Entries feed Comments feed Subscribe using your favourite web-based or desktop feed r...
  • Useful Linux Commands
    Find files older than 60 days find * -mtime +60 Delete files in backup folder which are older than 60 days rm -f `find /backup/ -mtime ...
  • Web Design Tips
    Nowadays, having a website for your business is like having a phone number. Almost every business has their own website, with their own doma...
  • Web Development Books
    Many developers often ask me what books I read and what books I recommend in regards to web development and web technologies such as AJAX, J...
  • Web Hosting Comparison Chart
    The following are some of the most popular web hosting providers. I have personally used the recommended ones. Hosting coupon codes will ...
  • Web Standards
    Web Standards is defined as a set of rules or specifications that should be followed when developing a website.  The main objectives o...
  • WordPress Themes
    All WordPress themes are designed and coded by esn studio, and licensed under GPL license. If you would like a customised WordPress theme fo...
  • Sam: Useful. I always forget the rel target one
  • Chronic Tinnitus: Hi, Thanks for taking the time to discuss a really confusing matter - I find Plesk so much more awkward than the cpanel that I'm used to. Thanks Chr
  • Serhiy: I personally liked the web hosting comparison chart. Especially the recommended *. I got Bluehost :) .-= Serhiy´s last blog ..

Interests

Web development, Web design, Open source technologies, Portal development, APIs, Web services, Social media applications, Search engine optimisation, Mobile application development, iPhone Apps, Web 2.0, Web 3.0, Latest Internet technologies

Misc.

  • bluehost Hosting $6.95/month
  • Joomla Templates