queryTask zoom to queryResult

3973
22
04-12-2016 05:19 AM
GoranTozievski
New Contributor

Hi im working on application that need to show linear route(static).Im getting the route but i cant make it to zoom to routeExtent.Here is my code.

require([

       "dojo/dom", "dojo/on",

       "esri/tasks/query", "esri/tasks/QueryTask", "dojo/domReady!"

        ], function (dom, on, Query, QueryTask) {

  var params = getUrlParams();

  var kiosk = params['kiosk'];

  var cel = params['cel'];

  var lang = params['lang'];

            queryTask = new esri.tasks.QueryTask("http://................................................................/MapServer/0");   

            var query = new Query();

            query.returnGeometry = true;

            query.outFields = [

              "*"

            ];

  var lang = 0;

  if(location.href.indexOf("_alb") > -1)

  lang =1;

  else if (location.href.indexOf("_en") > -1)

  lang =2;

  if(lang == '0'){

  queryTask = new esri.tasks.QueryTask("http://................................................................/MapServer/0 ");

  }else if(lang == '1'){

  queryTask = new esri.tasks.QueryTask("http://................................................................/MapServer/0 ");

  }

  else {

  queryTask = new esri.tasks.QueryTask("http://................................................................/MapServer/0 ");

  }

function executeRoute() {

  var nameKiosk ;

  if(kiosk == 1){

  nameKiosk = 1;

  }

  if(kiosk == 2){

  nameKiosk = 2;

  }

  if(lang == 0)

  query.where = "OD = '"+nameKiosk+"' and DO = '"+ime_objekt.innerText+"'";

  else if (lang == 1)

  query.where = "OD_AL = '"+nameKiosk+"' and DO_AL = '"+ime_objekt.innerText+"'";

  else

  query.where = "OD_EN = '"+nameKiosk+"' and DO_EN = '"+ime_objekt.innerText+"'";

  queryTask.execute(query, showResults);

  return false;

            }

  function getUrlParams() {

   var paramMap = {};

   if (location.search.length == 0) {

  return paramMap;

   }

   var parts = location.search.substring(1).split("&");

   for (var i = 0; i < parts.length; i ++) {

  var component = parts.split("=");

  paramMap [decodeURIComponent(component[0])] = decodeURIComponent(component[1]);

   }

   return paramMap;

  }

            function showResults(results) {

                var resultItems = [];

                var resultCount = results.features.length;

  var symbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 255, 255]), 4);

  for (var i = 0; i < resultCount; i++) {

                    var graphic = results.features;

                        graphic.setSymbol(symbol);

                        app.map.graphics.add(graphic);

  var stateExtent = graphic.geometry.getExtent();

  app.map.setExtent(stateExtent);

                }

                dom.byId("info").innerHTML = resultItems.join("");

                console.log(resultItems.join(""));

            }

        });

Thanks

0 Kudos
22 Replies
GoranTozievski
New Contributor

The query always will show only 1 result

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Goran,

  Looking at your code nothing jumps out as wrong. So does the graphic get added to the map but the map does not zoom? Are there any errors in your browsers web console?

0 Kudos
GoranTozievski
New Contributor

The code works fine and no errors apears.The problem is that i need the result to be zoomed on extent.

0 Kudos
GoranTozievski
New Contributor

Here is how works now.This is the line that is showed after query.

And i whant after the query the map to get the extend of the line like this(i made the zoom out manualy )

0 Kudos
thejuskambi
Occasional Contributor III

I have seen such behaviour, in case of tiled map service. But I still find your results odd for the fact that the feature is not centered.

You would need to expand the extent of the graphic to ensure that your result will be always shown.

var stateExtent = graphic.geometry.getExtent().expand(2);

Another way it to set the level of the map, greater than the scale of the graphic extent.

0 Kudos
GoranTozievski
New Contributor

Nothing happend after var stateExtent = graphic.geometry.getExtent().expand(2);    , and greater than the scale of the graphic extent its not an option for me atm

0 Kudos
DavidMeza
New Contributor II

It is strange that expanding the extent did not work for you. Have you tried a smaller value for the expand function and setting the fit option to true?

app.map.setExtent(stateExtent.expand(1.3),  true);

0 Kudos
TimWitt2
MVP Alum

       var graphic = results.features;

                        graphic.setSymbol(symbol);

                        app.map.graphics.add(graphic);

  var stateExtent = graphic.geometry.getExtent();

This is not really a  "graphic", so I don't think you can use setSymbol or getExtent on it. Could you show what one object of results.features, looks like?

What if you create a graphics layer and then add a new graphic for each results.feature?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Tim,

  A QueryTask complete event returns a FeatureSet which is an array of Graphics.

0 Kudos