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.

  1. // If the user has been here before, load the last point they were at
  2. // "testmap" is the name of this particular map - you'll need a unique
  3. // name for each map you make, per domain
  4. if ( ! map.loadPos("testmap") )
  5.   // If the user has never been here before, show the default point/zoom
  6.   map.centerAndZoom(new GPoint(-122.1419, 37.4419), 4);
  7.  
  8. // Update this link everytime the user moves/zooms the map
  9. map.updateLink( document.getElementById("link") );
  10.  
  11. // A sample marker
  12. var m = new GMarker( new GPoint(-122.1419, 37.4419) );
  13. map.addOverlay( m );
  14.  
  15. GEvent.addListener( m, "click", function() {
  16.   // Show an Info Window with a link in it
  17.   m.openLinkWindowHtml( "a test window!" );
  18. });

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

The Javascript file can be downloaded here

Posted: August 17th, 2005


If you particularly enjoy my work, I appreciate donations given with Gittip.

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.

Ukiyo-e Database and Search

Ukiyo-e.org

Japanese woodblock print database and search engine.


John Resig Twitter Updates

@jeresig

Infrequent, short, updates and links.