[mapserver-users] javascript query template examples

Steve Lime Steve.Lime at dnr.state.mn.us
Thu Jun 25 13:13:21 PDT 2009


Here's one example using new-style templating (recommend MapServer 5.4.1). We're querying some data
and building a function that can be used to populate a drop down list on a page. In this case I'm returning
a javascript file on-the-fly. You can also create javascript as part of a larger HTML page or just plain JSON
or GEOJSON output. Anyway, please ask questions if you need to.

The TEMPLATE:

// MapServer Template
function updateCountyList(element) {
  var option;

[resultset layer="counties"][feature]
  option = new Option("[CTY_NAME]", "[shpext format="$minx,$miny,$maxx,$maxy"  precision=0 expand="10%"]");
  try {
    element.add(option, null);
  } catch(e) {
    element.add(option);
  }[/feature][/resultset]
}

The OUTPUTFORMAT:

OUTPUTFORMAT
    NAME 'county_select_list'
    DRIVER 'TEMPLATE'
    MIMETYPE 'text/javascript'
    FORMATOPTION 'ATTACHMENT=county_select_list.js'
    FORMATOPTION 'FILE=/templates/demo_county_select_list.js'
END

The call to the CGI to run it is:

<script src="http://maps.dnr.state.mn.us/cgi-bin/mapserv54?map=demo.map&qlayer=counties&qformat=county_select_list&mode=nquery"></script>

The output:

function updateCountyList(element) {
  var option;

  option = new Option("Clearwater", "304963,5219538,337550,5326697");
  try {
    element.add(option, null);
  } catch(e) {
    element.add(option);
  }
  option = new Option("Aitkin", "434591,5106383,498803,5213702");
  try {
    element.add(option, null);
  } catch(e) {
    element.add(option);
  }
  option = new Option("Chisago", "486969,5013450,529584,5066587");
  try {
    element.add(option, null);
  } catch(e) {
    element.add(option);
  }
  option = new Option("Blue Earth", "387528,4852924,441106,4904495");
  try {
    element.add(option, null);
  } catch(e) {
    element.add(option);
  }
}


>>> On 6/25/2009 at 1:49 AM, in message
<200906250649.n5P6nCsN007609 at atom.scu.edu.au>, Greg Luker <gluker at scu.edu.au>
wrote:
> Hi mapserver-users,
> 
> Steve Lime has indeed explained bits of using javascript in CGI query 
> templates, but I'm still lost. Are there any examples showing how to 
> use javascript in a query template to do things like, for example, 
> format the returned query result list to remove duplicate records?
> 
> Thanks,
> Greg.
> 
> 
> Greg Luker
> GIS Lab Manager, Southern Cross University
> Lismore, AUSTRALIA.
> gluker at scu.edu.au 
> phone 61 2 66203026
> 
> 
> 
> _______________________________________________
> mapserver-users mailing list
> mapserver-users at lists.osgeo.org 
> http://lists.osgeo.org/mailman/listinfo/mapserver-users




More information about the MapServer-users mailing list