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("js", {
$.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 reponse 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"
]
}
geomapID
random generated ID of a map, a client can set a specific ID withgeomapID()
option.jsAssets
server returns the addresses of lefletjs resources. The array may contains the main echarts (leaflet.js
) and extra plugins javascript files.cssAssets
contains css assets required by lefletjs.jsCodeAssets
machbase-neo generates the javascript to properly render the lefletjs with the result data.
The HTML document below is an exmaple to utilize the JSON response above to render maps.
|
|
- Line 3, Pre-load lefletjs style sheet which is included in
cssAssets
fields in the above JSON response. - Line 6, Pre-load lefletjs library which is included in
jsAssets
fields 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. Becuase when a client requests a*.tql
file withGET
method, 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 javascrpt 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
.
|
|