Since Mike simultaneously outed me and out-did me and linked to the Processing folder of the Modest Maps source at the same time, I thought I'd better post a version of the library I've been working on so that I can stop thinking about it for a while.
Modest Maps is a BSD-licensed display and interaction library for tile-based maps in Flash (ActionScript 2.0 and ActionScript 3.0) and Python...
Because of restrictions on how Java applets load images from third-party domains, a Processing applet won't be able to access anyone else's image servers directly. I've resisted porting/implementing Modest Maps in Processing until now for that reason. However, thanks partly to my renewed interest in Processing and OpenGL, and thanks partly to a desire to re-implement the Actionscript 3 version of Modest Maps to take better advantage of matrix transforms, I've finally caved in and worked on it.
To the matrix transformation end, I've knocked together a little sketch that shows how to scroll around a tiled world that uses the same scheme as Google Maps: numbered zoom levels, rows and columns. Use the mouse to drag and the +/- keys to zoom in and out. Rows and columns start at zero in the top left, zoom level 0 has 1 row and column, zoom level 1 has 2, zoom level 2 has 4, zoom level 3 has 8, and so on in powers of 2 such that zoom level 17 has 131,072 rows and columns. At zoom level 17 that means there are (131,0722) or 17,179,869,184 tiles in the whole world, if you render them all (and you probably wouldn't)!
Once that bit is figured out, it's a case of getting hold of an image for each tile, and the logic for this is most succinctly expressed in the Modest Maps Python source code. I've ported that over to Processing with as few changes as possible, and a bit of testing and bit more thinking later I'm ready to offer up a test application (mac, windows, linux) to show what it's like so far:
There are some things that this version does, that the Flash one doesn't:
- it has reliable continuous scaling between zoom levels (use the mouse-wheel or press +/-, press 'z' to snap to the nearest pixel-perfect level)
- it tries to show the best image that it has for each zoom level (going up to one level in or as many levels out as it takes)
- it tries to load tiles from the centre of the map out to the edges
Sadly it also currently redraws everything on every frame (for 100% CPU UsageÃ¢âÂ¢, ask for it by name) Ã¢â¬â that will have to be fixed if we port this code back to Flash.
I'm not quite ready to distribute and support a library, but if you're not comfortable with grabbing the code from subversion and compiling for yourself, here's a zip file with a .jar file for your code folder and an example .pde file to get you started. I've thrown in some buttons too, no extra charge! The API is largely the same as the Actionscript 3 version: panning and zooming are solid, and it's easy to get the lat/lon location of points on the screen, and the point on the screen for lat/lon locations.
Update:I've updated the zip file above with a version that should work with Processing 0154. Let me know if there's a problem. the old one is here if you need it
Better documentation, animation (perhaps with Lee Byron's ShapeTween), event handling and such will have to wait for a future version: contributions and bug-fixes are welcome, feel free to leave a comment here if you'd like to get in touch.
The Final Word
Nobody from Google, Microsoft or Yahoo has given me, or you, permission to use their images within Processing. Apart from the fact that it won't work as an applet anyway, I should state clearly that I'm really only offering this library for personal use, and that it's covered by the same BSD license as the rest of Modest Maps, which comes with absolutely no warranties or guarantees. Caveat Coder!