Static views of CARTO maps can be generated using the Static Maps API within CARTO.js. The map’s style, including the zoom and bounding box, follows from what was set in the viz.json
file, but you can change the zoom, center, and size of your image with a few lines of code. You can also change your basemap Images can be placed in specified DOM elements on your page, or you can generate a URL for the image.
The easiest way to generate an image is by using the following piece of code, which generates is replaced by an img
tag once run in an HTML file:
1
2
3
4
5
6
7
8
9
<script>
var vizjson_url = 'https://documentation.carto.com/api/v2/viz/008b3ec6-02c3-11e4-b687-0edbca4b5057/viz.json';
cartodb.Image(vizjson_url)
.size(600, 400)
.center([-3.4, 44.2])
.zoom(4)
.write({ class: "thumb", id: "AwesomeMap" });
</script>
1
<img id="AwesomeMap" src="https://cartocdn-ashbu.global.ssl.fastly.net/documentation/api/v1/map/static/center/04430594691ff84a3fdac56259e5180b:1419270587670/4/-3.4/44.2/600/400.png" class="thumb">
Name | Description |
---|---|
layerSource | can be either a viz.json object or a MapConfig object.Note: If defining an image through the MapConfig layer definition, you must set the tiler_domain , tiler_port , and tiler_protocol , as displayed in this example. Otherwise the Static Image API tries to use your localhost to source the tiles and an error appears. |
options | |
---|---|
|_ basemap | change the basemap specified in the layer definition. Type: Object defining base map properties (see example below). |
|_ no_cdn | Disable CDN usage. Type: Boolean. Default: false (use CDN) |
|_ override_bbox | Override default of using the bounding box of the visualization. This is needed to use Image.center and Image.zoom . Type: Boolean. Default: false (use bounding box) |
An Image
object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script>
var vizjson_url = 'https://documentation.carto.com/api/v2/viz/008b3ec6-02c3-11e4-b687-0edbca4b5057/viz.json';
var basemap = {
type: "http",
options: {
urlTemplate: "http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png",
subdomains: ["a", "b", "c"]
}
};
cartodb.Image(vizjson_url, {basemap: basemap})
.size(600, 400)
.center([0,0])
.write({ class: "thumb", id: "AwesomeMap" });
</script>
Sets the size of the image.
Name | Description |
---|---|
width | the width of the resulting image in pixels |
height | the height of the resulting image in pixels |
An Image
object
Sets the center of the map.
Name | Description |
---|---|
latLng | an array of the latitude and longitude of the center of the map. Example: [40.4378271, -3.6795367] |
An Image
object
Sets the zoom level of the static map. Must be used with the option override_bbox: true
if not using Image.center
or Image.bbox
.
Name | Description |
---|---|
zoomLevel | the zoom of the resulting static map. zoomLevel must be an integer in the range [0,24]. |
An Image
object
If you set bbox
, center
and zoom
will be overridden.
Name | Description |
---|---|
boundingBox | an array of coordinates making up the bounding box for your map. boundingBox takes the form: [sw_lat, sw_lon, ne_lat, ne_lon] . |
An Image
object
Inserts the image into the HTML DOM element specified.
Name | Description |
---|---|
HTMLImageElement | the DOM element where your image is to be located. |
An Image
object
1
cartodb.Image(vizjson_url).into(document.getElementById('map_preview'))
Adds an img
tag in the same place script is executed. It’s possible to specify a class name (class
) and/or an id attribute (id
) for the resulting image:
Name | Description |
---|---|
class | the DOM class applied to the resulting img tag. |
id | the DOM id applied to the resulting img tag. |
src | path to a temporary image that acts as a placeholder while the static map is retrieved. |
An Image
object
1
2
3
4
5
6
7
<script>
cartodb.Image(vizjson_url)
.size(600, 400)
.center([-3.4, 44.2])
.zoom(10)
.write({ class: "thumb", id: "ImageHeader", src: 'spinner.gif' });
</script>
Gets the URL for the image requested.
Name | Description |
---|---|
err | error associated with the image request, if any. |
url | URL of the generated image. |
An Image
object
1
2
3
4
5
6
7
<script>
cartodb.Image(vizjson_url)
.size(600, 400)
.getUrl(function(err, url) {
console.log('image url',url);
})
</script>
Gets the URL for the image requested.
Name | Description |
---|---|
format | image format of resulting image. One of png (default) or jpg (which have a quality of 85 dpi) |
An Image
object