One Week of Google Maps – Part 1

Starting today – and going until next week – I will be writing a post, everyday, detailing a chunk of Google Maps API-enhancing code that I’ve written. During the past couple weeks I’ve been working a number of contract jobs – all of which have centered around the usage of the Google Maps API, a powerful tool for programmers. As I’ve gone along, there have been a number of features that I’ve developed (or found) that I think other programmers would benefit from knowing. Today is going to be a double-whamy.

First: Geocoding addresses for Google Maps. This is probably the first thing that people ask for when using the API. Thankfully there are plenty of free services available that can be harnessed to make Geocoding a reality. I wrote a chunk of Javascript (with an associated Perl script) a little while ago that acheives this goal. Unfortunately, some people still seem to have trouble finding it – so it deserves another link to. Feel free to read [link] the full release for more information.

You can view a demo of this modification here.

Second: Click Zoom This enhancement is so simple – I love it. In a nutshell: You can now click anywhere on a map to zoom in. Thankfully, you can still grab-drag the map around without being effected. After having implemented it in one particular application, I’ve become addicted to it – it’s so handy! I use it by simply clicking the name of a city (or location) that I want to jump to. It’s so much easier then re-centering and clicking the ‘Zoom’ button every step of the way. The code is short, so feel free to give it a try:

GEvent.addListener( map, "click", function(o,p) {
  if ( o != null ) return;
  if ( map.getZoomLevel() > 1 )
    map.centerAndZoom( p, map.getZoomLevel() - 2 );
  else if ( map.getZoomLevel() == 1 )
    map.centerAndZoom( p, 0 );
});

I have the code set to zoom in two levels every time you click, but this can be tweaked to be less aggressive. At some point, I think it would be neat to add ‘Right Click Zooms Out’ functionality – unless, of course, someone beats me too it.

You can view a demo of this modification here.

Posted: August 12th, 2005


Subscribe for email updates

20 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.