One Week of Google Maps – Part 3


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

When I was planning tonight’s post I was having all sorts of fun looking at optomizing the addOverlay function. This function, when used again and again with hundreds of points slows things down very quickly. The code, which is all the rage on the Google Maps API mailing list, can be found at the above page (copied below for completeness):

  1. GMap.prototype.addOverlays = function( a ) {
  2.   for ( var i = 0; i < a.length; i++ ) {
  3.     this.overlays.push( a[i] );
  4.     a[i].initialize( this );
  5.     a[i].redraw( true );
  6.   }
  7.   this.reOrderOverlays();
  8. };

One problem, though – this feature is no longer going to be needed in the new version of the API. Which is a very good thing. So, that killed my original article for tonight. However, a new topic just so happened to fall in my lap: Advanced Mouse Control.

The one hack that I really like, that was posted to the Google Maps API mailing list, was the ability to zoom in using your middle scroll wheel. It feels incredibly natural and is a lot of fun to play with – you can view a demo here.

After doing some more digging around I realized that no one has built any sort of good, natural, support for the following mouse actions: Double Clicking, Right Clicking, and Middle Clicking. So, I went about to do just that. I waded through the API code for a while and finally acheived a result that I like. All of the events can be used with absolutely no extra code, observe:

  1. var map = new GMap(document.getElementById("map"));
  2. map.addControl(new GSmallMapControl());
  3. map.centerAndZoom(new GPoint(-122.4419, 37.4419), 4);
  4.  
  5. GEvent.addListener( map, "dblclick", function(p){
  6.   map.addOverlay( new GMarker( p ) );
  7. });
  8. GEvent.addListener( map, "rightclick", function(p){
  9.   alert( "Right!" );
  10. });
  11. GEvent.addListener( map, "middleclick", function(p){
  12.   alert( "Middle!" );
  13. });

As you can see, the events behave exactly as a ‘click’ event would – except no overlay information is passed in the arguments. The only argument passed along is the current lat/long of where the click was made – from which a lot can be done.

One functional change that I made was to disable the ‘pan’ animation that occurs when you normally double-click. If you attach any listener to “dblclick” then that ‘pan’ will no longer fire – which is a smart move, in my opinion.

To make the above code work, all you have to do is include this file in your header, just after you include the Google Maps API, like this:

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

And that’s it – you’ll be ready to go from there! If you’re interested in viewing a very quick demo, using the above code, click here.

Posted: August 15th, 2005


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

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