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:
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):
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);
});




February 9th, 2010 at 4:49 pm
RT @aNieto2k: [link to post] <– Rotando mapas con CSS3 y jQuery (via @ajaxian)
– Posted using Chat Catcher
February 9th, 2010 at 5:27 pm
Nice one, really good idea and probably the only workable one for css3 rotate :)
February 9th, 2010 at 5:34 pm
[link to post] <– Rotando mapas con CSS3 y jQuery (via @ajaxian)
– Posted using Chat Catcher
February 9th, 2010 at 5:35 pm
RT @aNieto2k: [link to post] <– Rotando mapas con CSS3 y jQuery (via @ajaxian)
– Posted using Chat Catcher
February 9th, 2010 at 5:51 pm
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 ;-)
February 9th, 2010 at 7:40 pm
RT @vvladescu: Rotating maps with CSS3 and jQuery [link to post]
– Posted using Chat Catcher
February 9th, 2010 at 8:35 pm
Rotating maps with CSS3 and jQuery [link to post]
– Posted using Chat Catcher
February 9th, 2010 at 8:46 pm
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
February 10th, 2010 at 2:35 am
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.
February 10th, 2010 at 2:49 am
“Rotating maps with CSS3 and jQuery” [link to post]
– Posted using Chat Catcher
February 10th, 2010 at 9:32 am
Isn’t working correctly on Firefox, moving problems after rotation and “=” which supposed to reset my map it’s actually zooming it.
February 10th, 2010 at 10:06 am
RT @omone: Rotating maps with CSS3 and jQuery [link to post] #css3 #jquery
– Posted using Chat Catcher
February 10th, 2010 at 10:08 am
Rotating maps with CSS3 and jQuery [link to post] #css3 #jquery
– Posted using Chat Catcher
February 10th, 2010 at 10:46 pm
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.
February 11th, 2010 at 1:34 am
WHAT!!!! Rotating maps with CSS3 and jQuery [link to post]
– Posted using Chat Catcher
February 11th, 2010 at 5:36 am
RT @jagir: RT @aextnet: WHAT!!!! Rotating maps with CSS3 and jQuery [link to post]
– Posted using Chat Catcher
February 11th, 2010 at 12:47 pm
Another proof that machine translation sucks – check http://is.gd/89lum vs. [link to post]
– Posted using Chat Catcher
February 11th, 2010 at 1:33 pm
RT @codepo8: Another proof that machine translation sucks – check http://is.gd/89lum vs. [link to post]
– Posted using Chat Catcher
February 14th, 2010 at 1:21 am
Deadly, always knew jquery could be used to greatly enhance mapping apps.