Viewing an older version of MapBox.js. Check out v1.5.0 for the latest.

Marker movement

Select all
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Marker movement</title>
  
  <script src='http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.js'></script>
  <link href='http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.css' rel='stylesheet' />
  
  <style>
    body { margin:0; padding:0; }
    #map { position:absolute; top:0; bottom:0; width:100%; }
  </style>
</head>
<body>
<div id='map'></div>
<script>
    // GeoJSON input features
    // The image and url properties of the features will be used in
    // the tooltips
    var features = [{
        "geometry": { "type": "Point", "coordinates": [0, 0]},
        "properties": { "id": 1 }
    }];

    // Create map
    var map = mapbox.map('map');
    map.addLayer(mapbox.layer().id('examples.map-vyofok3q'));

    // Create and add marker layer
    var markerLayer = mapbox.markers.layer()
    // Assign the initial features
    .features(features)
    // This is the important API in this example: .key sets a key getter
    // function that ensures that re-calling .features() with moved
    // features just moves the elements rather than removing and
    // redrawing them.
    .key(function(f) {
        return f.properties.id;
    });

    var t = 0;
    window.setInterval(function() {
        // making a lissajous curve here just for fun. this isn't necessary
        features[0].geometry.coordinates[0] = Math.cos(t * 0.5) * 50;
        features[0].geometry.coordinates[1] = Math.sin(t) * 50;
        // Reassign the features
        markerLayer.features(features);
        t += 0.1;
    }, 50);

    map.addLayer(markerLayer);

    // Set iniital center and zoom
    map.centerzoom({
        lat: 0,
        lon: 0
    }, 3);

    // Attribute map
    map.ui.attribution.add()
        .content('<a href="http://mapbox.com/about/maps">Terms &amp; Feedback</a>');
</script>
The code and documentation to mapbox.js is hosted on GitHub where you can contribute changes and improvements.