2015-10-22 21:02:01 +08:00
# Quickstart
2015-10-22 19:28:08 +08:00
2016-02-19 02:15:28 +08:00
## Anonymous Maps
2015-10-22 19:28:08 +08:00
2016-02-19 02:15:28 +08:00
Here is an example of how to create an Anonymous Map with JavaScript:
2015-10-22 19:28:08 +08:00
```javascript
var mapconfig = {
"version": "1.3.1",
"layers": [{
"type": "cartodb",
"options": {
"cartocss_version": "2.1.1",
"cartocss": "#layer { polygon-fill: #FFF ; }",
"sql": "select * from european_countries_e"
}
}]
}
$.ajax({
crossOrigin: true,
type: 'POST',
dataType: 'json',
contentType: 'application/json',
2016-03-22 23:23:04 +08:00
url: 'https://{username}.cartodb.com/api/v1/map',
2015-10-22 19:28:08 +08:00
data: JSON.stringify(mapconfig),
success: function(data) {
2016-03-22 23:23:04 +08:00
var templateUrl = 'https://{username}.cartodb.com/api/v1/map/' + data.layergroupid + '/{z}/{x}/{y}.png'
2015-10-22 19:28:08 +08:00
console.log(templateUrl);
}
})
```
2016-02-19 02:15:28 +08:00
## Named Maps
2015-10-22 19:28:08 +08:00
2016-02-19 02:15:28 +08:00
Let's create a Named Map using some private tables in a CartoDB account.
2015-10-22 19:28:08 +08:00
The following map config sets up a map of European countries that have a white fill color:
```javascript
{
"version": "0.0.1",
"name": "test",
"auth": {
"method": "open"
},
"layergroup": {
"layers": [{
"type": "mapnik",
"options": {
"cartocss_version": "2.1.1",
"cartocss": "#layer { polygon-fill: #FFF ; }",
"sql": "select * from european_countries_e"
}
}]
}
}
```
2016-02-19 02:15:28 +08:00
The MapConfig needs to be sent to CartoDB's Map API using an authenticated call. Here we will use a command line tool called `curl` . For more info about this tool, see [this blog post ](http://quickleft.com/blog/command-line-tutorials-curl ), or type `man curl` in bash. Using `curl` , and storing the config from above in a file `MapConfig.json` , the call would look like:
2015-10-28 22:51:31 +08:00
#### Call
2015-10-22 19:28:08 +08:00
```bash
2016-03-22 21:19:50 +08:00
curl 'https://{username}.cartodb.com/api/v1/map/named?api_key={api_key}' -H 'Content-Type: application/json' -d @mapconfig .json
2015-10-22 19:28:08 +08:00
```
To get the `URL` to fetch the tiles you need to instantiate the map, where `template_id` is the template name from the previous response.
2015-10-28 22:51:31 +08:00
#### Call
2015-10-22 19:28:08 +08:00
```bash
2016-03-22 21:19:50 +08:00
curl -X POST 'https://{username}.cartodb.com/api/v1/map/named/{template_id}' -H 'Content-Type: application/json'
2015-10-22 19:28:08 +08:00
```
The response will return JSON with properties for the `layergroupid` , the timestamp (`last_updated`) of the last data modification and some key/value pairs with `metadata` for the `layers` .
2015-10-28 22:51:31 +08:00
2015-10-22 19:28:08 +08:00
Note: all `layers` in `metadata` will always have a `type` string and a `meta` dictionary with the key/value pairs.
2015-10-28 22:51:31 +08:00
#### Response
2015-10-22 19:28:08 +08:00
```javascript
{
"layergroupid": "c01a54877c62831bb51720263f91fb33:0",
"last_updated": "1970-01-01T00:00:00.000Z",
"metadata": {
"layers": [
{
"type": "mapnik",
"meta": {}
}
]
}
}
```
You can use the `layergroupid` to instantiate a URL template for accessing tiles on the client. Here we use the `layergroupid` from the example response above in this URL template:
```bash
2016-03-22 23:23:04 +08:00
https://{username}.cartodb.com/api/v1/map/{layergroupid}/{z}/{x}/{y}.png
2015-10-22 19:28:08 +08:00
```