[mapserver-users] Preparing/modifying a querymap for furtherzoom/pan operations...

Jim.Haug at state.sd.us Jim.Haug at state.sd.us
Thu Nov 27 23:20:45 EST 2008


Hello Nick. I was stumped by the same thing for a time. I solved it by running the query twice. It feels kludgy, but it works--I'll clean it up later. The first time through is an itemnquery, which does an AJAX call to a template. That returns a set of JSON variables from the template (all I need are the new map extents). These are then stored in the mapserve object extents, following which I rebuild the query as an itemnquerymap. This produces the map of the same query with the qstring items highlighted. I can then pan, zoom, etc. from there. Here's the code I used in the main file. Much of it is shamelessly stolen from better coders than I. The JSON setup in the template is pretty straightforward. I've put it below the javascript. Note that the routines that build the URLs use different mapfiles. One is tailored just for JSON, while the other handles regular queries.

--Jim Haug

// javascript code to do itemnsearch and itemnsearchmap.

// we will be doing searches using ajax/json to provide data. function item_search(),
// which is called from form 'searchform' in the document,
// will set up an itemquery url to send to the server. It will then call
// function getData(url), which will make an ajax call using the itemquery url.
// Data will be returned from the template in json format, which will allow
// function onResponse() to parse it to variables.

  function item_search() {
    var query_url = "";
    var layerlist = ms.getLayers('+');
    if (document.searchform.qitem.value == "site_no") {
      qitem = "site_no";
      qlayer = "sites";
    } else if (document.searchform.qitem.value == "archive") {
      qitem = "archive";
      qlayer = "surveys";
    } else if (document.searchform.qitem.value == "histdist") {
      qitem = "reg";
      qlayer = "hist-dist";
    } else if (document.searchform.qitem.value == "histdistr") {
      qitem = "reg";
      qlayer = "hist-dist-restrict";
    } else if (document.searchform.qitem.value == "bridges") {
      qitem = "propertyna";
      qlayer = "bridges";
    } else if (document.searchform.qitem.value == "cemeteries") {
      qitem = "propertyna";
      qlayer = "cemeteries";
    } else if (document.searchform.qitem.value == "structures") {
      qitem = "propertyna";
      qlayer = "structures";
    } else { // must be trs, since it's the last choice in the form
      qitem = "arms_plss";
      qlayer = "sd_plss";
    }
    qstring = "/" + document.searchform.qstring.value + "/i";
    query_url = ms.mapserver +
               '?mode=itemnquery' + 
               '&map=' + query_mapfile +
               '&qlayer=' + qlayer +
               '&qitem=' + qitem +
               '&qstring=' + qstring +
               '&mapext=shapes' +
               '&mapsize=' + ms.width + '+' + ms.height +
               '&layers=' + layerlist +
               ms.options;
    getData(query_url);
  }

//  this.setLayer = function(name, status) {
//    self.layers[name] = status;
//  }


//--------------> start of itemquery search functions using ajax/json

  var myrequest;

  function getData(url) {
    myrequest=null;
    if (window.XMLHttpRequest) { // code for IE7, Firefox, Mozilla, etc.
      myrequest=new XMLHttpRequest();
    }
    else if (window.ActiveXObject) { // code for IE5, IE6
      myrequest=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (myrequest!=null) {
      myrequest.onreadystatechange=onResponse;
      myrequest.open("GET",url,true);
      myrequest.send(null);
    }
    else {
      alert("Your browser does not support XMLHTTP.\nYou need to upgrade");
    }
  }

  function onResponse() {
    if(myrequest.readyState!=4) return;
    if(myrequest.status!=200) {
      alert("Problem retrieving data");
      return;
    }
    if (myrequest.responseText.indexOf("msQuery") != -1) {
      alert("No item found in search");
    }
    else {
      // parse the returned query data
      var my_ms = eval('(' + myrequest.responseText + ')');
      // all we really need are the extents (minx and y, max x and y) in order
      // to set up a new map to be drawn

      // make sure the page is ready to display a map
      ms.mode = "map";
      // buffer the coordinates by 1000 map units (set to meters in map file)
      minx = parseFloat(my_ms.minx) - 1000;
      miny = parseFloat(my_ms.miny) - 1000;
      maxx = parseFloat(my_ms.maxx) + 1000;
      maxy = parseFloat(my_ms.maxy) + 1000;
      ms.setExtent(minx, miny, maxx, maxy);
      var layerlist = ms.getLayers('+');
      if (document.searchform.qitem.value == "site_no") {
        qitem = "site_no";
        qlayer = "sites";
      } else if (document.searchform.qitem.value == "archive") {
        qitem = "archive";
        qlayer = "surveys";
      } else if (document.searchform.qitem.value == "histdist") {
        qitem = "reg";
        qlayer = "hist-dist";
      } else if (document.searchform.qitem.value == "histdistr") {
        qitem = "reg";
        qlayer = "hist-dist-restrict";
      } else if (document.searchform.qitem.value == "bridges") {
        qitem = "propertyna";
        qlayer = "bridges";
      } else if (document.searchform.qitem.value == "cemeteries") {
        qitem = "propertyna";
        qlayer = "cemeteries";
      } else if (document.searchform.qitem.value == "structures") {
        qitem = "propertyna";
        qlayer = "structures";
      } else { // must be trs, since it's the last choice in the form
        qitem = "arms_plss";
        qlayer = "sd_plss";
      }
      qstring = "/" + document.searchform.qstring.value + "/i";
      if(ms.referencemap) {
        ms.referencemap.url = ms.mapserver +
                            '?mode=reference' +
                            '&map=' + ms.referencemap.mapfile +
                            '&mapext=' + ms.extent.join('+') +
                            '&mapsize=' + ms.width + '+' + ms.height;  
      }
      ms.url = ms.mapserver +
              '?mode=itemnquerymap' + 
              '&map=' + query_mapfile +
              '&qlayer=' + qlayer +
              '&qitem=' + qitem +
              '&qstring=' + qstring +
              '&mapext=' + ms.extent.join('+') +
              '&mapsize=' + ms.width + '+' + ms.height +
              '&layers=' + layerlist +
              ms.options;
      ms_draw();
    }
  }

//--------> end of itemquery search functions


=========================================
JSON code from queryfooter.html

{
  "minx": "[minx]",
  "miny": "[miny]",
  "maxx": "[maxx]",
  "maxy": "[maxy]",
}



-----Original Message-----
From: mapserver-users-bounces at lists.osgeo.org on behalf of nikos at maich.gr
Sent: Thu 11/27/2008 3:22 PM
To: mapserver-users at lists.osgeo.org
Subject: [mapserver-users] Preparing/modifying a querymap for furtherzoom/pan operations...
 
Hello all,

I'm trying to use a generated query map (itemnquery) and insert it back
into my main MS interface foe further processing, e.g zoom/pan/query...
My map is generated with &mapext=shapes

I'm making an Ajax call to MS, and returning a pointer to the generated
querymap, and updating my map.

This part works...

Problem is that when I then go to zom/pan/query, the results are not what
are expected, e.g my form still has the "before query" instance/variables
and takes off from there, meaning it does not for instance zoom to the
iamge displayed

would appreciate some help,

tia,

nick boretos

_______________________________________________
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