Legacy

Mapbox.js is no longer in active development. To learn more about our newer mapping tools see Mapbox GL JS.

Static map from GeoJSON with geo-viewport and geojson-extent

Use geo-viewport and geojson-extent to find a static map that shows two markers.

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Static map from GeoJSON with geo-viewport and geojson-extent</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.mapbox.com/mapbox.js/v3.3.1/mapbox.js'></script>
<link href='https://api.mapbox.com/mapbox.js/v3.3.1/mapbox.css' rel='stylesheet' />
<style>
  body { margin:0; padding:0; }
  #map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<img id='static-map'></div>

<script src='https://api.mapbox.com/mapbox.js/plugins/geo-viewport/v0.4.1/geo-viewport.js'></script>
<script src='https://api.mapbox.com/mapbox.js/plugins/geojson-extent/v1.0.0/geojson-extent.js'></script>
<script>
L.mapbox.accessToken = '<your access token here>';

// This is an example using geojson-extent. See the full docs for full details
// https://github.com/mapbox/geojson-extent

// Declare a GeoJSON file with the two places we're interested in.
var sfAndDc = {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          -122.41722106933594,
          37.78672476186115
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          -77.03201293945312,
          38.91133881927709
        ]
      }
    }
  ]
};

// Calculate a bounding box in west, south, east, north order.
var bounds = geojsonExtent(sfAndDc);

// The size of the desired map.
var size = [750, 400];

// Calculate a zoom level and centerpoint for this map.
var vp = geoViewport.viewport(bounds, size, 0, 19, 512);

// Create pins at places in the geojson file.
var pins = [];
for (var i = 0; i < sfAndDc.features.length; i++) {
    pins.push('pin-s(' + sfAndDc.features[i].geometry.coordinates.join(',') + ')');
}

document.getElementById('static-map').src =
    'https://api.mapbox.com/styles/v1/mapbox/streets-v11/static/' +
    pins.join(',') + '/' +
    vp.center.join(',') + ',' +
    vp.zoom-- + '/' +
    size.join('x') +
    '?access_token=' + L.mapbox.accessToken;
</script>
to create your own custom map and use it in this example.
Use this example by copying its source into your own HTML page and replacing the Map ID with one of your own from your projects. Having trouble with JavaScript? Try out Codecademy or contact our support team.
Copy example