Windshaft-cartodb/docs/named_maps.md

473 lines
12 KiB
Markdown
Raw Normal View History

2016-01-30 01:07:47 +08:00
# Named Maps
2015-10-22 19:28:08 +08:00
Named maps are essentially the same as anonymous maps except the MapConfig is stored on the server, and the map is given a unique name. You can create named maps from private data, and users without an API Key can view your Named Map (while keeping your data private). The Named map workflow consists of making a call to your database, referencing a table, inserting your variables into the template where placeholders are defined, and creating custom queries.
2015-10-22 19:28:08 +08:00
The main two differences compared to anonymous maps are:
- **auth layer**
This allows you to control who is able to see the map based on a token auth
- **templates**
Since the MapConfig is static it can contain some variables so the client can modify the map's appearance using those variables.
Template maps are persistent with no preset expiration. They can only be created or deleted by a CartoDB user with a valid API_KEY (see auth section).
2016-01-30 00:52:17 +08:00
**Note:** There is a limit of 4,096 named maps allowed per account. If you need to create more Named maps, it is recommended to use templates.
2016-01-07 22:33:16 +08:00
2015-10-22 21:02:01 +08:00
## Create
2015-10-22 19:28:08 +08:00
2015-10-28 23:26:33 +08:00
#### Definition
2015-10-22 19:28:08 +08:00
```html
POST /api/v1/map/named
```
2015-10-28 23:26:33 +08:00
#### Params
Params | Description
--- | ---
api_key | is required
2015-10-22 19:28:08 +08:00
2015-10-28 23:26:33 +08:00
#### template.json
2015-10-22 19:28:08 +08:00
```javascript
{
"version": "0.0.1",
"name": "template_name",
"auth": {
"method": "token",
"valid_tokens": [
"auth_token1",
"auth_token2"
]
},
"placeholders": {
"color": {
"type": "css_color",
"default": "red"
},
"cartodb_id": {
"type": "number",
"default": 1
}
},
"layergroup": {
"version": "1.0.1",
"layers": [
{
"type": "cartodb",
"options": {
"cartocss_version": "2.1.1",
"cartocss": "#layer { polygon-fill: <%= color %>; }",
"sql": "select * from european_countries_e WHERE cartodb_id = <%= cartodb_id %>"
}
}
]
},
"view": {
"zoom": 4,
"center": {
"lng": 0,
"lat": 0
},
"bounds": {
"west": -45,
"south": -45,
"east": 45,
"north": 45
}
}
}
```
2015-10-22 21:02:01 +08:00
#### Arguments
2015-10-22 19:28:08 +08:00
2015-10-28 23:26:33 +08:00
Params | Description
--- | ---
name | There can be at most _one_ template with the same name for any user. Valid names start with a letter or a number, and only contain letters, numbers, dashes (-) or underscores (_).
auth |
--- | ---
&#124;_ method | `"token"` or `"open"` (the default if no `"method"` is given).
&#124;_ valid_tokens | when `"method"` is set to `"token"`, the values listed here allow you to instantiate the named map.
placeholders | Variables not listed here are not substituted. Variables not provided at instantiation time trigger an error. A default is required for optional variables. Type specification is used for quoting, to avoid injections see template format section below.
layergroup | the layer list definition. This is the MapConfig explained in anonymous maps. See [MapConfig File Format](http://docs.cartodb.com/cartodb-platform/maps-api/mapconfig/) for more info.
2015-10-28 23:26:33 +08:00
view (optional) | extra keys to specify the compelling area for the map. It can be used to have a static preview of a named map without having to instantiate it. It is possible to specify it with `center` + `zoom` or with a bounding box `bbox`. Center+zoom takes precedence over bounding box.
--- | ---
&#124;_ zoom | The zoom level to use
&#124;_ center |
--- | ---
&#124;_ &#124;_ lng | The longitude to use for the center
&#124;_ &#124;_ lat | The latitude to use for the center
&#124;_ bounds |
--- | ---
&#124;_ &#124;_ west | LowerCorner longitude for the bounding box, in decimal degrees (aka most western)
&#124;_ &#124;_ south | LowerCorner latitude for the bounding box, in decimal degrees (aka most southern)
&#124;_ &#124;_ east | UpperCorner longitude for the bounding box, in decimal degrees (aka most eastern)
&#124;_ &#124;_ north | UpperCorner latitude for the bounding box, in decimal degrees (aka most northern)
2015-10-22 19:28:08 +08:00
2015-10-22 21:02:01 +08:00
### Template Format
2015-10-22 19:28:08 +08:00
A templated `layergroup` allows the use of placeholders in the "cartocss" and "sql" elements of the "option" object in any "layer" of a `layergroup` configuration
Valid placeholder names start with a letter and can only contain letters, numbers, or underscores. They have to be written between the `<%=` and `%>` strings in order to be replaced.
2015-10-22 21:02:01 +08:00
#### Example
2015-10-22 19:28:08 +08:00
```javascript
<%= my_color %>
```
The set of supported placeholders for a template will need to be explicitly defined with a specific type and default value for each.
2015-10-22 21:02:01 +08:00
### Placeholder Types
2015-10-22 19:28:08 +08:00
The placeholder type will determine the kind of escaping for the associated value. Supported types are:
2015-10-28 23:26:33 +08:00
Types | Description
--- | ---
sql_literal | internal single-quotes will be sql-escaped
sql_ident | internal double-quotes will be sql-escaped
number | can only contain numerical representation
css_color | can only contain color names or hex-values
2015-10-22 19:28:08 +08:00
Placeholder default values will be used whenever new values are not provided as options at the time of creation on the client. They can also be used to test the template by creating a default version with new options provided.
When using templates, be very careful about your selections as they can give broad access to your data if they are defined losely.
2015-10-28 23:26:33 +08:00
#### Call
2015-10-22 19:28:08 +08:00
```html
curl -X POST \
-H 'Content-Type: application/json' \
-d @template.json \
'https://documentation.cartodb.com/api/v1/map/named?api_key=APIKEY'
```
2015-10-28 23:26:33 +08:00
#### Response
2015-10-22 19:28:08 +08:00
```javascript
{
"template_id":"name",
}
```
2015-10-22 21:02:01 +08:00
## Instantiate
2015-10-22 19:28:08 +08:00
Instantiating a map allows you to get the information needed to fetch tiles. That temporal map is an anonymous map.
2015-10-28 23:26:33 +08:00
#### Definition
2015-10-22 19:28:08 +08:00
```html
POST /api/v1/map/named/:template_name
```
2015-10-28 23:26:33 +08:00
#### Param
2015-10-22 19:28:08 +08:00
2015-10-28 23:26:33 +08:00
Param | Description
--- | ---
auth_token | optional, but required when `"method"` is set to `"token"`
2015-10-22 19:28:08 +08:00
```javascript
// params.json
{
"color": "#ff0000",
"cartodb_id": 3
}
```
The fields you pass as `params.json` depend on the variables allowed by the named map. If there are variables missing it will raise an error (HTTP 400)
2015-10-29 00:31:20 +08:00
- **auth_token** *optional* if the named map needs auth
2015-10-22 21:02:01 +08:00
### Example
2015-10-22 19:28:08 +08:00
You can initialize a template map by passing all of the required parameters in a POST to `/api/v1/map/named/:template_name`.
Valid credentials will be needed if required by the template.
2015-10-28 23:26:33 +08:00
#### Call
2015-10-22 19:28:08 +08:00
```bash
curl -X POST \
-H 'Content-Type: application/json' \
-d @params.json \
'https://documentation.cartodb.com/api/v1/map/named/@template_name?auth_token=AUTH_TOKEN'
```
2015-10-28 23:26:33 +08:00
#### Response
2015-10-22 19:28:08 +08:00
```javascript
{
"layergroupid": "docs@fd2861af@c01a54877c62831bb51720263f91fb33:123456788",
"last_updated": "2013-11-14T11:20:15.000Z"
}
```
2015-10-28 23:26:33 +08:00
#### Error
2015-10-22 19:28:08 +08:00
```javascript
{
"errors" : ["Some error string here"]
}
```
2015-10-28 23:26:33 +08:00
You can then use the `layergroupid` for fetching tiles and grids as you would normally (see anonymous map section). However you'll need to show the `auth_token`, if required by the template.
2015-10-22 19:28:08 +08:00
2015-10-22 21:02:01 +08:00
## Using JSONP
2015-10-22 19:28:08 +08:00
There is also a special endpoint to be able to initialize a map using JSONP (for old browsers).
2015-10-28 23:26:33 +08:00
#### Definition
2015-10-22 19:28:08 +08:00
```bash
GET /api/v1/map/named/:template_name/jsonp
```
2015-10-28 23:26:33 +08:00
#### Params
Params | Description
--- | ---
auth_token | optional, but required when `"method"` is set to `"token"`
config | Encoded JSON with the params for creating named maps (the variables defined in the template)
lmza | This attribute contains the same as config but LZMA compressed. It cannot be used at the same time than `config`.
callback | JSON callback name
2015-10-22 19:28:08 +08:00
2015-10-28 23:26:33 +08:00
#### Call
2015-10-22 19:28:08 +08:00
```bash
curl 'https://documentation.cartodb.com/api/v1/map/named/:template_name/jsonp?auth_token=AUTH_TOKEN&callback=callback&config=template_params_json'
```
2015-10-28 23:26:33 +08:00
#### Response
2015-10-22 19:28:08 +08:00
```javascript
callback({
"layergroupid":"c01a54877c62831bb51720263f91fb33:0",
"last_updated":"1970-01-01T00:00:00.000Z"
"cdn_url": {
"http": "http://cdb.com",
"https": "https://cdb.com"
}
})
```
This takes the `callback` function (required), `auth_token` if the template needs auth, and `config` which is the variable for the template (in cases where it has variables).
```javascript
url += "config=" + encodeURIComponent(
JSON.stringify({ color: 'red' });
```
The response is in this format:
```javascript
callback({
layergroupid: "dev@744bd0ed9b047f953fae673d56a47b4d:1390844463021.1401",
last_updated: "2014-01-27T17:41:03.021Z"
})
```
2015-10-22 21:02:01 +08:00
## Update
2015-10-22 19:28:08 +08:00
2015-10-28 23:26:33 +08:00
#### Definition
2015-10-22 19:28:08 +08:00
```bash
PUT /api/v1/map/named/:template_name
```
2015-10-28 23:26:33 +08:00
#### Params
2015-10-22 19:28:08 +08:00
2015-10-28 23:26:33 +08:00
Param | Description
--- | ---
api_key | is required
2015-10-22 19:28:08 +08:00
2015-10-28 23:26:33 +08:00
#### Response
2015-10-22 19:28:08 +08:00
Same as updating a map.
2015-10-22 21:02:01 +08:00
### Other Info
2015-10-22 19:28:08 +08:00
Updating a named map removes all the named map instances so they need to be initialized again.
2015-10-22 21:02:01 +08:00
### Example
2015-10-22 19:28:08 +08:00
2015-10-28 23:26:33 +08:00
#### Call
2015-10-22 19:28:08 +08:00
```bash
curl -X PUT \
-H 'Content-Type: application/json' \
-d @template.json \
'https://documentation.cartodb.com/api/v1/map/named/:template_name?api_key=APIKEY'
```
2015-10-28 23:26:33 +08:00
#### Response
2015-10-22 19:28:08 +08:00
```javascript
{
"template_id": "@template_name"
}
```
If any template has the same name, it will be updated.
If a template with the same name does NOT exist, a 400 HTTP response is generated with an error in this format:
```javascript
{
"errors" : ["error string here"]
}
```
2015-10-22 21:02:01 +08:00
## Delete
2015-10-22 19:28:08 +08:00
Delete the specified template map from the server and it disables any previously initialized versions of the map.
2015-10-28 23:26:33 +08:00
#### Definition
2015-10-22 19:28:08 +08:00
```bash
DELETE /api/v1/map/named/:template_name
```
2015-10-28 23:26:33 +08:00
#### Params
2015-10-22 19:28:08 +08:00
2015-10-28 23:26:33 +08:00
Param | Description
--- | ---
api_key | is required
2015-10-22 19:28:08 +08:00
2015-10-22 21:02:01 +08:00
### Example
2015-10-22 19:28:08 +08:00
2015-10-28 23:26:33 +08:00
#### Call
2015-10-22 19:28:08 +08:00
```bash
curl -X DELETE 'https://documentation.cartodb.com/api/v1/map/named/:template_name?api_key=APIKEY'
```
2015-10-28 23:26:33 +08:00
#### Response
2015-10-22 19:28:08 +08:00
```javascript
{
"errors" : ["Some error string here"]
}
```
On success, a 204 (No Content) response will be issued. Otherwise a 4xx response with an error will be returned.
2015-10-22 21:02:01 +08:00
## Listing Available Templates
2015-10-22 19:28:08 +08:00
This allows you to get a list of all available templates.
2015-10-28 23:26:33 +08:00
#### Definition
2015-10-22 19:28:08 +08:00
```bash
GET /api/v1/map/named/
```
2015-10-28 23:26:33 +08:00
#### Params
2015-10-22 19:28:08 +08:00
2015-10-28 23:26:33 +08:00
Param | Description
--- | ---
api_key | is required
2015-10-22 19:28:08 +08:00
2015-10-22 21:02:01 +08:00
### Example
2015-10-22 19:28:08 +08:00
2015-10-28 23:26:33 +08:00
#### Call
2015-10-22 19:28:08 +08:00
```bash
curl -X GET 'https://documentation.cartodb.com/api/v1/map/named?api_key=APIKEY'
```
2015-10-28 23:26:33 +08:00
#### Response
2015-10-22 19:28:08 +08:00
```javascript
{
"template_ids": ["@template_name1","@template_name2"]
}
```
2015-10-28 23:26:33 +08:00
#### Error
2015-10-22 19:28:08 +08:00
```javascript
{
2015-10-28 23:26:33 +08:00
"errors" : ["Some error string here"]
2015-10-22 19:28:08 +08:00
}
```
2015-10-22 21:02:01 +08:00
## Getting a Specific Template
2015-10-22 19:28:08 +08:00
This gets the definition of a template.
2015-10-28 23:26:33 +08:00
#### Definition
2015-10-22 19:28:08 +08:00
```bash
GET /api/v1/map/named/:template_name
```
2015-10-28 23:26:33 +08:00
#### Params
2015-10-22 19:28:08 +08:00
2015-10-28 23:26:33 +08:00
Param | Description
--- | ---
api_key | is required
2015-10-22 19:28:08 +08:00
2015-10-22 21:02:01 +08:00
### Example
2015-10-22 19:28:08 +08:00
2015-10-28 23:26:33 +08:00
#### Call
2015-10-22 19:28:08 +08:00
```bash
curl -X GET 'https://documentation.cartodb.com/api/v1/map/named/:template_name?api_key=APIKEY'
```
2015-10-28 23:26:33 +08:00
#### Response
2015-10-22 19:28:08 +08:00
```javascript
{
"template": {...} // see template.json above
}
```
2015-10-28 23:26:33 +08:00
#### Error
2015-10-22 19:28:08 +08:00
```javascript
{
"errors" : ["Some error string here"]
}
```
2016-01-30 01:11:21 +08:00
## Use CartoDB.js to Create Named Maps
2015-10-22 19:28:08 +08:00
Named maps can be used with CartoDB.js by specifying a named map in a layer source as follows. Named maps are treated almost the same as other layer source types in most other ways.
```js
var layerSource = {
user_name: '{your_user_name}',
type: 'namedmap',
named_map: {
name: '{template_name}',
layers: [{
layer_name: "layer1",
interactivity: "column1, column2, ..."
}]
}
}
cartodb.createLayer('map_dom_id',layerSource)
.addTo(map_object);
```
2015-10-28 23:46:50 +08:00
[CartoDB.js](http://docs.cartodb.com/cartodb-platform/cartodb-js/) has methods for accessing your named maps.
2015-10-22 19:28:08 +08:00
2015-10-28 23:46:50 +08:00
1. [layer.setParams()](http://docs.cartodb.com/cartodb-platform/cartodb-js/api-methods/#layersetparamskey-value) allows you to change the template variables (in the placeholders object) via JavaScript
2. [layer.setAuthToken()](http://docs.cartodb.com/cartodb-platform/cartodb-js/api-methods/#layersetauthtokenauthtoken) allows you to set the auth tokens to create the layer
### Complete Examples of Named Maps created with CartoDB.js
- [Named map selectors with interaction](http://bl.ocks.org/ohasselblad/515a8af1f99d5e690484)
- [Named map with interactivity and config file used to create it](http://bl.ocks.org/ohasselblad/d1a45b8ff5e7bd90cd68)
- [Toggling sublayers in a Named Map](http://bl.ocks.org/ohasselblad/c1a0f4913610eec53cd3)