MapView goTo() not working in search.on("search-complete")

290
3
Jump to solution
03-14-2023 01:45 AM
PL1
by
New Contributor II

I am using Arcgis/core version 4.24.

I have the following code inside view.when():

 

    let polylineSymbol = {
      type: "simple-line",  // autocasts as SimpleLineSymbol()
      color: [226, 119, 40],
      width: 4
    };

    function createGraphic(line: any) {
      let polylineGraphic = new Graphic({
        geometry: line,
        symbol: polylineSymbol
      });
      return polylineGraphic;
    }

    function measureLine(vertices: any) {
    
      let line = createLine(vertices);
      let graphic = createGraphic(line);
      view.graphics.add(graphic);
    }

    search.on("search-complete", function(event: any) {

      event.results[0].results.forEach((result: any) => {
        measureLine(result.feature.geometry.paths)
      })
      // here I am getting just one graphic but to no avail.
      let xmax = view.graphics.getItemAt(0).geometry.extent.xmax;
      let ymax = view.graphics.getItemAt(0).geometry.extent.ymax;
      let xmin = view.graphics.getItemAt(0).geometry.extent.xmin;
      let ymin = view.graphics.getItemAt(0).geometry.extent.ymin;

      const extent = new Extent({
        xmax,
        ymax,
        xmin,
        ymin,
        spatialReference: { wkid: view.spatialReference.wkid }
      })
      view.goTo({
        extent
      }).catch((error) => {
        console.log('result', error)
      })
    });

 

 But for some reason the code above does not work. Any suggestion on why the goTo() does not zoom into the extent?

0 Kudos
1 Solution

Accepted Solutions
PL1
by
New Contributor II

Hello, and thank you for replying to my question!

I actually solved the problem by this way:

 

  let polylineSymbol = {
    type: "simple-line",  // autocasts as SimpleLineSymbol()
    color: COLOR_CYAN,
    width: HIGHLIGHT_WIDTH
  };
  
  reactiveUtils.once(() => featurelayer?.loadStatus === "loaded")
    .then(async () => {
      addToSearchSources(featurelayer) // function that adds the layer to sources
      const result = await search.search(searchParam);
      const graphics = result.results[0].results.map((res: any) => {
        res.feature.symbol = polylineSymbol;
        return res.feature;
      });
      graphicsLayer.addMany(graphics);
      view.map.add(graphicsLayer);
      view.goTo(graphics);
    });

 

 

I also set the Search widget's autoSelect to false.

View solution in original post

0 Kudos
3 Replies
PL1
by
New Contributor II

I changed the code so that I use 

view.goTo(view.graphics);

 

But goTo() zooms to the first search result. This is not wanted. I would like to zoom on all of the graphics, instead of the first one.  

0 Kudos
UndralBatsukh
Esri Regular Contributor

This actually (view.goTo(view.graphics) is the one of the preferred way to zooming into array of graphics. You don't have to do what you are doing in your first post. 

I tested this with 4.24 and the MapView.goTo is working as expected. Here is a simple codepen shows It working: https://codepen.io/U_B_U/pen/wvEmEzw?editors=1000 

 

 

 

PL1
by
New Contributor II

Hello, and thank you for replying to my question!

I actually solved the problem by this way:

 

  let polylineSymbol = {
    type: "simple-line",  // autocasts as SimpleLineSymbol()
    color: COLOR_CYAN,
    width: HIGHLIGHT_WIDTH
  };
  
  reactiveUtils.once(() => featurelayer?.loadStatus === "loaded")
    .then(async () => {
      addToSearchSources(featurelayer) // function that adds the layer to sources
      const result = await search.search(searchParam);
      const graphics = result.results[0].results.map((res: any) => {
        res.feature.symbol = polylineSymbol;
        return res.feature;
      });
      graphicsLayer.addMany(graphics);
      view.map.add(graphicsLayer);
      view.goTo(graphics);
    });

 

 

I also set the Search widget's autoSelect to false.

0 Kudos