Dynamically Drawing a Line

Adding points to a line in an animation
Select all
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Dynamically Drawing a Line</title>
  
  <meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no' />
  <script src='//api.tiles.mapbox.com/mapbox.js/v1.5.0/mapbox.js'></script>
  <link href='//api.tiles.mapbox.com/mapbox.js/v1.5.0/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>
var map = L.mapbox.map('map', 'examples.map-9ijuk24y')
    .setView([0, 0], 3);

// add a new line to the map, but one with no points - yet
var polyline = L.polyline([]).addTo(map);

// keep a tally of how many points we've added to the map
var pointsAdded = 0;

// start adding new points to the map
add();

function add() {

    // addLatLng takes a new latLng location and puts it at the end of the
    // line. You could pull points from your data or generate them - this
    // example just makes a sine wave with some math.
    polyline.addLatLng(
        L.latLng(
            Math.cos(pointsAdded / 20) * 30,
            pointsAdded));

    // pan the map along with where the line is being added. optional, of course
    map.setView([0, pointsAdded], 3);

    // then, if we haven't already added a lot of points, queue up the page
    // to call add() in a 100 milliseconds
    if (++pointsAdded < 360) window.setTimeout(add, 100);
}
</script>
The code and documentation to mapbox.js is hosted on GitHub where you can contribute changes and improvements.