Skip to main content
100%

SVG slippy map using Viewbox with D3

✓ Published0🌍 Public
ZZJONSSON
Last edited Oct 5, 2011
Created on Oct 5, 2011

An interactive slippy map displays CloudMade tiles inside an SVG element, with D3.js managing tile loading and viewBox coordinates. Panning and zooming manipulate the SVG viewBox directly, keeping tile coordinates static. The svg_interact module handles mouse events and wheel zoom, while svg_map calculates visible tiles and fetches them as raster images via CloudMade's tile API. The map supports continuous zoom levels and only renders tiles within the current viewport.

AI-generated description

(hold shift when scrolling the mouse-wheel for now)

This is a simple slippy map that keeps the absolute coordinates of all tiles/location static, but moves the SVG viewbox as the user pans and zooms around. We can then draw SVG objects directly on the coordinate system and do not have to update location when the viewBox is changed.

There are two components to this, both standalone.

  1. svg_interact: provides pan and zoom to any svg-object by moving the viewBox
  2. svg_map: a tiler that watches the viewbox location and size at regular intervals and fetches the appropriate cloudmake tiles to cover the viewbox with some extra padding. This tiler uses the .data() method to enter and exit tiles. Another alternative might be to keep an array and push old items out as new come in. We need to discard old tiles as number of visible tiles affects performance. The graphic contents are however automatically cached by the browser.

I would really appreciate helpful hints on making this better and more d3 compatible (i.e. using pv.behavior to get better mouse functions)

Similar vizzes