goTo issues with zooming

663
2
Jump to solution
02-08-2022 11:15 PM
Olivier_MOUGEL
New Contributor II

I try to understand the folowing error :
TypeError: Cannot read properties of null (reading 'targetGeometry')
in my code.
I just define a featurelayer :

       const secteur = new FeatureLayer({
          title:"Secteurs des collèges",
          definitionExpression: expr,
          popupTemplate: popup,
          labelingInfo: [labelClass]
        });
        map.add(secteur);

        secteur.when(() => {
        zoomToLayer(secteur);
        });
 
and this function :

function zoomToLayer(layer) {
          return layer.queryExtent().then((response) => {
            view.goTo(response.extent)
          });
        }
 
The definition expression retrieves a single polygon in the view without zooming on it.
Where have I to define a target geometry ?
Thank's for some help

0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Frequent Contributor

I am going to guess that your basemap is a different spatial reference than your service. When you query against the service, the SR will be the original SR of the service. You can. request the geometries to be converted to match your map like this.

 

function zoomToLayer(layer) {
  // create query from layer
  const query = layer.createQuery();
  // ask for SR to match the View
  query.outSpatialReference = view.spatialReference;
  return layer.queryExtent(query).then((response) => {
    view.goTo(response.extent)
  });
}

View solution in original post

2 Replies
ReneRubalcava
Frequent Contributor

I am going to guess that your basemap is a different spatial reference than your service. When you query against the service, the SR will be the original SR of the service. You can. request the geometries to be converted to match your map like this.

 

function zoomToLayer(layer) {
  // create query from layer
  const query = layer.createQuery();
  // ask for SR to match the View
  query.outSpatialReference = view.spatialReference;
  return layer.queryExtent(query).then((response) => {
    view.goTo(response.extent)
  });
}
Olivier_MOUGEL
New Contributor II

Thank's a lot. It's true I use a local SR for my feature layer 
Have a nice day 

0 Kudos