Embed in HTML
Save the code below as basic_map.tql and we will show you how to embed the result of this TQL into web page.
SCRIPT({
$.yield({
type:"polygon",
coordinates:[[37,-109.05],[41,-109.03],[41,-102.05],[37,-102.05]],
properties: {
fill: false
}
});
$.yield({
type: "marker",
coordinates:[38.9934,-105.5018],
properties: {
popup:{content: Date()}
}
});
$.yield({
type: "circleMarker",
coordinates:[38.935,-105.520],
properties:{ radius: 40, fillOpacity:0.4, stroke: false }
});
})
GEOMAP()IFRAME
| |
JSON Response
Call .tql script file with a custom HTTP header X-Tql-Output: json
to produce the result in JSON instead of full HTML document,
so that caller can embed the chart into any place of the HTML DOM.
The X-Tql-Output: json header is actually equivalent to the GEOMAP() SINK with geoMapJson(true) option like GEOMAP( geoMapJson(true))).
When the response of /db/tql/basic_map.tql is JSON, it contains required addresses of the result javascript.
{
"geomapID":"MTcwMzE3NjYwMjA0Nzg1NjY0",
"style": {
"width": "600px",
"height": "600px",
"grayscale": 0
},
"jsAssets": ["/web/geomap/leaflet.js"],
"cssAssets": ["/web/geomap/leaflet.css"],
"jsCodeAssets": [
"/web/api/tql-assets/MTcwMzE3NjYwMjA0Nzg1NjY0_opt.js",
"/web/api/tql-assets/MTcwMzE3NjYwMjA0Nzg1NjY0.js"
]
}geomapIDrandom generated ID of a map, a client can set a specific ID withgeomapID()option.jsAssetsserver returns the addresses of lefletjs resources. The array may contains the main echarts (leaflet.js) and extra plugins javascript files.cssAssetscontains css assets required by lefletjs.jsCodeAssetsmachbase-neo generates the javascript to properly render the lefletjs with the result data.
The HTML document below is an example to utilize the JSON response above to render maps.
| |
- Line 3, Pre-load lefletjs style sheet which is included in
cssAssetsfields in the above JSON response. - Line 6, Pre-load lefletjs library which is included in
jsAssetsfields in above response example. - Line 17, The HTTP header
X-Tql-Output: json. So that machbase-neo TQL engine generates a JSON containing meta information of map instead of full HTML document. Because when a client requests a*.tqlfile withGETmethod, machbase-neo generates HTML document by default. - Line 26, Load js files into the HTML DOM tree that are generated and replied in
jsCodeAssets.
Dynamic TQL
The api /db/tql can receive POSTed TQL script and produces the result in javascript.
Caller side javascript can load the result javascript dynamically as the example below.
In this example, the geomapID() (line 20) is provided and the document has a <div> with the same id.
| |
Loading Sequence Problem
In the examples above, if we tried to load the both of jsAssets and jsCodeAssets dynamically, like below code for example.
| |
There must be some loading sequence issue, because the lefletjs library in obj.jsAssets might not be completely loaded
before obj.jsCodeAssets are loaded.
To avoid the problem of loading sequence, it can be fixed like below code.
Add load event listener to enable callback for load-completion.
| |
When the last jsAssets loaded, start to load jsCodeAssets.
| |