Rotating maps with CSS3 and jQuery

One thing that annoys the heck out of me with maps you see on a screen is that you can’t rotate them. When I look at a real map I constantly turn it around so that I face in the right direction. Google maps lately “added this feature”:http://code.google.com/apis/maps/documentation/examples/aerial-simple.html in the hybrid and satellite maps but I wanted to do that with the simple maps and also other map providers.

The solution was CSS3. With the rotation transformations you can arbitarily turn elements. Of course this differs again from browser to browser which is why it made sense to me to find a library plugin that does that. “Zachary Johnson”:http://www.zachstronaut.com/posts/2009/08/07/jquery-animate-css-rotate-scale.html build one of those and using this together with the Google Maps API it was pretty easy to build a rotating map:

Rotating a map with CSS3 and jQuery by  you.

The code itself is very easy – thanks to the transformation work already done in Zach’s code:

google.load("maps", "2.x");
   function initialize() {
     var mapcontainer = $('#mapcontainer');
     var mapdiv = $('#map');
     var geocoder = new GClientGeocoder();
     var map = new google.maps.Map2(mapdiv);
     map.addControl(new GSmallMapControl());
     map.addControl(new GMapTypeControl());
     map.setCenter(new google.maps.LatLng(37.4419, -122.1419), 13);
     mapcontainer.after('<div id="buttons">'+
                        '<form id="f"><label for="loc">Location:</label>'+
                        '<input type="text" id="loc">'+
                        '<input type="submit" value="go"></form>'+
                        '<p>Press R and L to rotate map, = to reset.</p>'+
                        '<p>Use cursor keys to move.</p>'+
                        '<p>Zoom with + and -.</p>'+
                        '</div>');
     mapcontainer.attr('tabIndex','-1');
     mapcontainer.focus();
     $('#f').submit(function(event){
       var value = $('#loc').attr('value');
       if (geocoder) {
          geocoder.getLatLng(
            value,
            function(point) {
              if (!point) {
                alert(value + " not found");
              } else {
                map.setCenter(point, 13);
                var marker = new GMarker(point);
                map.addOverlay(marker);
                mapcontainer.focus();
              }
            }
          );
        }
       return false;
     });
     mapcontainer.keydown(function(event){
       switch(event.keyCode){
         case 82: mapdiv.animate({rotate: '+=5deg'}, 0); break;
         case 76: mapdiv.animate({rotate: '-=5deg'}, 0); break;
         case 40: map.panDirection(0,-1); break;
         case 38: map.panDirection(0,1); break;
         case 39: map.panDirection(-1,0); break;
         case 37: map.panDirection(1,0); break;
         case 107: map.setZoom(map.getZoom()+1); break;
         case 109: map.setZoom(map.getZoom()-1); break;
         case 61: mapdiv.animate({rotate: '0'}, 0); break;
       }
     });
   }
   google.setOnLoadCallback(initialize);

Of course I was not the first with this. The “StreetView Fun”:http://github.com/cheeaun/streetview-fun demo by Lim Chee Aun (“@cheeaun”:http://twitter.com/cheeaun) has a similar implementation (click “maps” in the demo to see it):

StreetView Fun by  you.

Things to fix

  • Map navigation: as you might have already experienced, dragging the map is becoming very confusing. This cannot really be solved as it would require Google Maps to know the rotation. Right now I am using the panDirection method to move the map with the cursor keys – a cleaner way would be to use PanTo and really calculate the next place to pan to taking into consideration the angle of the map. Any volunteers?
  • I am quite sure I am violating Google’s terms and conditions with this as I am cropping off the copyright.I found a way to display the copyright and Google branding:
GEvent.addListener(map, "tilesloaded", function() {
 var logo = $('#logocontrol');
 logo.attr('style','');
 var copyright = $('#map div[dir=ltr]');
 copyright.attr('style','');
 $('#mapinfo').append(logo);
 $('#mapinfo').append(copyright);
});

Tags: , , , ,

19 Responses to “Rotating maps with CSS3 and jQuery”

  1. carrero (David Carrero Fdez-B) Says:






    RT @aNieto2k: [link to post] <– Rotando mapas con CSS3 y jQuery (via @ajaxian)

    Posted using Chat Catcher

  2. Abban Says:

    Nice one, really good idea and probably the only workable one for css3 rotate :)

  3. aNieto2k (Andrés Nieto) Says:






    [link to post] <– Rotando mapas con CSS3 y jQuery (via @ajaxian)

    Posted using Chat Catcher

  4. AlcidesRC (Alcides Ramos) Says:






    RT @aNieto2k: [link to post] <– Rotando mapas con CSS3 y jQuery (via @ajaxian)

    Posted using Chat Catcher

  5. Marc Says:

    Wait what? We guys are capable of rotating maps in our minds, that’s why we are good at reading maps. With this code you make it possible for women to read maps, are you sure you want to unleash this onto the web?

    (nice work ;-)

  6. AndreiPintica (swaMp) Says:






    RT @vvladescu: Rotating maps with CSS3 and jQuery [link to post]

    Posted using Chat Catcher

  7. vvladescu (Val Vl?descu) Says:






    Rotating maps with CSS3 and jQuery [link to post]

    Posted using Chat Catcher

  8. zmarties Says:

    This doesn’t work for me in Firefox, since the keystrokes get passed to the “search as you type” function (aka Quick Find) instead of driving the map.

    The fix for this is simple – add “return false” to the end of the mapcontainer.keydown function

  9. Jason Says:

    Great article. This works well for mapping utilities that do not need to show properly oriented labels. If you want to show horizontal labels or custom overlays, you could use this to rotate a tiled map base layer underneath a canvas or SVG-based drawing layer for the custom components.

  10. cheeaun (Lim Chee Aun) Says:






    “Rotating maps with CSS3 and jQuery” [link to post]

    Posted using Chat Catcher

  11. Eugen Says:

    Isn’t working correctly on Firefox, moving problems after rotation and “=” which supposed to reset my map it’s actually zooming it.

  12. web2feed (Michael Davis) Says:






    RT @omone: Rotating maps with CSS3 and jQuery [link to post] #css3 #jquery

    Posted using Chat Catcher

  13. omone (Frank Matuse) Says:






    Rotating maps with CSS3 and jQuery [link to post] #css3 #jquery

    Posted using Chat Catcher

  14. Ryan Moore Says:

    http://geekportfolio.com/rotmap.html doesn’t use your code, but fixes the rotation problem – code is ass-ugly, but you can get the gist of how it works.

  15. aextnet (AEXT.NET Magazine) Says:






    WHAT!!!! Rotating maps with CSS3 and jQuery [link to post]

    Posted using Chat Catcher

  16. stevie258 (Steve Johnathan) Says:






    RT @jagir: RT @aextnet: WHAT!!!! Rotating maps with CSS3 and jQuery [link to post]

    Posted using Chat Catcher

  17. codepo8 (Christian Heilmann) Says:






    Another proof that machine translation sucks – check http://is.gd/89lum vs. [link to post]

    Posted using Chat Catcher

  18. ashalynd (Anna Nachesa) Says:






    RT @codepo8: Another proof that machine translation sucks – check http://is.gd/89lum vs. [link to post]

    Posted using Chat Catcher

  19. James Says:

    Deadly, always knew jquery could be used to greatly enhance mapping apps.

Wait till I come! is the blog of Christian Heilmann , a developer evangelist living and working in London, England. Download vcard.

Feed me, Seymour: Entries (RSS) and Comments (RSS).