Overview Examples Documentation Download

© 2010 CloudMade, OpenStreetMap contributors, CCBYSA. Ball by Dmitry Baranovskiy.

Shadows & Gradients

Polymap’s built-in support for GeoJSON provides basic rendering of geometric primitives including points, lines and polygons. Sometimes, however, you want to take advantage of the raw power of SVG! Polymaps produces a clean and well-behaved SVG tree, allowing rich graphical effects to be applied through post-processing with JavaScript and CSS.

Inspired by Raphaël’s playful “Ball” example, here we create a custom translucent marker. Complex lighting effects are simulated with two radial gradients (one for a surface highlight, and a second for the internal reflection). An additional drop shadow is created using a Gaussian blur filter. The marker scales automatically with zoom level, such that it appears constant-size over the map!

The map background is an image layer from CloudMade. Register a developer account with CloudMade for your own API key.

This example uses a little helper library called NNS for creating SVG elements. NNS is optional and not required to use Polymaps. We chose to implement this example with NNS to simplify the creation of custom SVG elements.

The n$ method converts a DOM element to a wrapper, which has convenience methods for setting attributes (attr) and creating child elements (add). Other libraries, including jQuery, provide similar functionality.

Source Code

var po = org.polymaps;

var svg = n$("#map").add("svg:svg");

var map = po.map()
    .center({lat: 37.787, lon: -122.228})

    + "/1a1b06b230af4efdbb989ea99e9841af" // http://cloudmade.com/register
    + "/998/256/{Z}/{X}/{Y}.png")
    .hosts(["a.", "b.", "c.", ""])));

    .features([{geometry: {coordinates: [-122.258, 37.805], type: "Point"}}])
    .on("load", load));


/* Create a shadow filter. */
    .attr("id", "shadow")
    .attr("width", "140%")
    .attr("height", "140%")
    .attr("in", "SourceAlpha")
    .attr("stdDeviation", 3);

/* Create radial gradient r1. */
    .attr("id", "r1")
    .attr("fx", 0.5)
    .attr("fy", 0.9)
    .attr("offset", "0%")
    .attr("stop-color", "#00bf17")
    .attr("offset", "100%")
    .attr("stop-color", "#0f2f13");

/* Create radial gradient r2. */
    .attr("id", "r2")
    .attr("fx", 0.5)
    .attr("fy", 0.1)
    .attr("offset", "0%")
    .attr("stop-color", "#cccccc")
    .attr("offset", "100%")
    .attr("stop-color", "#cccccc")
    .attr("stop-opacity", 0);

/** Post-process the GeoJSON points and replace them with shiny balls! */
function load(e) {
  var r = 20 * Math.pow(2, e.tile.zoom - 12);
  for (var i = 0; i < e.features.length; i++) {
    var c = n$(e.features[i].element),
        g = c.parent().add("svg:g", c);

    g.attr("transform", "translate(" + c.attr("cx") + "," + c.attr("cy") + ")");

        .attr("r", r)
        .attr("transform", "translate(" + r + ",0)skewX(-45)")
        .attr("opacity", .5)
        .attr("filter", "url(#shadow)");

        .attr("fill", "url(#r1)")
        .attr("r", r)
        .attr("cx", null)
        .attr("cy", null));

        .attr("transform", "scale(.95,1)")
        .attr("fill", "url(#r2)")
        .attr("r", r);
Polymaps is a project from SimpleGeo and Stamen.