In this step you learn
  • The groups of helpers in ExtjsUtils, and what each is for.
  • How to reach the map itself (OpenLayers) and move it.
  • Where to look up any helper.

The functions in your query reach the platform through ExtjsUtils, a toolbox of helpers grouped by subject. This example uses three of them: messages and questions, a layer’s legend, and the map itself.

Click the picture to run it live - nothing is saved.
The query 43 lines · runs as is
// The ExtjsUtils toolbox: the platform's helpers your code calls - here to talk to the reader, read a layer's legend and move the map.
// ExtjsUtils.ALERTIFY shows messages and questions, ExtjsUtils.LAYER reads layers and legends,
// ExtjsUtils.JS.getMap() is the map itself (OpenLayers), ExtjsUtils.QUERY applies, adds and
// removes layers. The API reference lists every helper of each group.
ExtjsUtils.QUERY.setQueryGlobalProperties({
  REGIONS: {
    North: [-74, -10, -46, 5],
    Northeast: [-48, -18, -34, -1],
    South: [-58, -34, -47, -22],
  },
}) && [
  {
    title: "Elevation",
    name: "CSR:altimetria",
    source: "calculate",
    visibility: true,
    opacity: 0.8,
    paramsButtonConfig: [{ type: "query", pressed: true }],
    descriptionHtml:
      "{{button|id=read_legend|text=Read the legend|handler=readLegend}}" +
      "{{button|id=choose_region|text=Go to a region|handler=chooseRegion}}",
    functions: {
      readLegend: function () {
        var entries = ExtjsUtils.LAYER.getLayerLegend(this, 0) // the legend of this layer's first map
          .filter(function (entry) { return String(entry.title).trim().toLowerCase() !== "null"; }); // without the no-data class
        ExtjsUtils.ALERTIFY.log("<b>" + entries.length + " classes</b>, from " +
          entries[0].title + " to " + entries[entries.length - 1].title + ".");
      },
      chooseRegion: function () {
        ExtjsUtils.ALERTIFY.confirmChoice(
          "Which region should the map show?",
          Object.keys(REGIONS).map(function (name) { return { label: name, value: name }; }),
          function (name) {
            var map = ExtjsUtils.JS.getMap();
            var box = REGIONS[name];
            map.zoomToExtent(new OpenLayers.Bounds(box[0], box[1], box[2], box[3])
              .transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()));
          }
        );
      },
    },
  },
];

The helpers you will use most

GroupForExamples
ExtjsUtils.QUERYThe query and its layerssetQueryGlobalProperties, addLayer, removeLayer, addRemoteWMSServer, decorate, setMappiaIoCallback, postMessage
ExtjsUtils.ALERTIFYTalking to the readerlog (a passing message), alert, confirmChoice (a question with options)
ExtjsUtils.LAYERLayers and legendsgetLayerByName, getLayerLegend
ExtjsUtils.JSThe page’s main objectsgetMap() - the map, an OpenLayers map
ExtjsUtils.ZOOMZoomingzoomToExtent
ExtjsUtils.REQUESTThe page address and the networkgetParameterByName (a parameter of the page URL), advToolsOptions (a page option)
ExtjsUtils.GEOJSONVector datageojson2Features, shapefile2GeojsonAsync
ExtjsUtils.CSSPage stylesdefineClass
ExtjsUtils.CONFIGURATIONQuery-wide settingssetOptions

The map itself

ExtjsUtils.JS.getMap() returns the map, an OpenLayers 2 map: setCenter, zoomToExtent, getZoom, its layers. Its coordinates are in the map’s projection (metres), so a longitude and latitude are converted first, as the example does:

var map = ExtjsUtils.JS.getMap();
var box = new OpenLayers.Bounds(-48, -18, -34, -1)            // west, south, east, north, in degrees
  .transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
map.zoomToExtent(box);

Every helper, with its parameters and an example, is in the API reference. ExtjsUtils.QUERY and ExtjsUtils.CONFIGURATION are under Query setup, because they are mostly called before the list; the others are under ExtjsUtils helpers, by name - ALERTIFY, LAYER, JS… A layer’s own settings are not helpers: they are its properties (step 2).

Try this

  1. Add a region to REGIONS, for example Southeast: [-53, -25, -39, -14].
  2. Show the legend in a dialog: ExtjsUtils.ALERTIFY.alert(...) instead of log.
  3. Log the map’s zoom after choosing a region: ExtjsUtils.JS.getMap().getZoom().

Read more