One Week of Google Maps – Part 5

This is Part 5 of a week long series featuring code for the Google Maps API.

After loading address latitude/longitude information, the next most desired feature for a Google Maps developer is the ability, for the end-user, to link to a particular map that they’re viewing. I’ve taken this concept one step further:

  • A link, to the current view of the map, can be provided to the user. Additionally, the link can be automatically updated everytime the user moves the map or zooms in/out.
  • The last view that a user was looking at, on your map, is automatically loaded the next time they visit your site.
  • A new info window is available which, when revealed, provides the user with a way to link to that specific marker’s coordinates.

These features, once combined, add a lot of much-needed usability to the API. Using this code is fairly foolproof too. Below is a chunk of code taken from the demo, demonstrating all the important features of this addition to the API.

// If the user has been here before, load the last point they were at
// "testmap" is the name of this particular map - you'll need a unique
// name for each map you make, per domain
if ( ! map.loadPos("testmap") )
  // If the user has never been here before, show the default point/zoom
  map.centerAndZoom(new GPoint(-122.1419, 37.4419), 4);

// Update this link everytime the user moves/zooms the map
map.updateLink( document.getElementById("link") );

// A sample marker
var m = new GMarker( new GPoint(-122.1419, 37.4419) );
map.addOverlay( m );

GEvent.addListener( m, "click", function() {
  // Show an Info Window with a link in it
  m.openLinkWindowHtml( "a test window!" );
});

To view a demo of these features, please click here.

The Javascript file can be downloaded here

Posted: August 17th, 2005


Subscribe for email updates

68 Comments (Show Comments)



Comments are closed.
Comments are automatically turned off two weeks after the original post. If you have a question concerning the content of this post, please feel free to contact me.


Secrets of the JavaScript Ninja

Secrets of the JS Ninja

Secret techniques of top JavaScript programmers. Published by Manning.

John Resig Twitter Updates

@jeresig / Mastodon

Infrequent, short, updates and links.