Having a problem with goTo

4793
7
05-20-2016 04:02 PM
RyanKelso
Occasional Contributor III

I'm working on a very simple map just as a learning exercise.  I want it to select, highlight, and zoom to a defined parcel.  It works, except that some of the time I get this error:  "TypeError:  Cannot read property 'wkid' of null".  I get the error when the MapView.goTo() method tries to fire, but the graphics are visible on the map.

I'm a beginner trying to learn, so consider that when checking out the code.  My parcel data is not public so I swapped it out for King County data below, but oddly I can't get the error to occur with this other data.  Any ideas about what's going on?

require([
      "esri/Map",
      "esri/PopupTemplate",
      "esri/views/MapView",
      "esri/layers/MapImageLayer",
      "esri/layers/GraphicsLayer",
      "esri/symbols/SimpleFillSymbol",
      "esri/tasks/QueryTask",
      "esri/tasks/support/Query",
      "dojo/_base/array",
      "dojo/domReady!"
    ], function(Map, PopupTemplate, MapView, MapImageLayer, GraphicsLayer, SimpleFillSymbol, QueryTask, Query, arrayUtils) {


      var parcelsMapService = MapImageLayer({
        url:  "http://gismaps.kingcounty.gov/arcgis/rest/services/Property/KingCo_PropertyInfo/MapServer",
        sublayers: [
          {
            id: 2,
            visible: true
          }
        ]
      });


      var resultsLayer = new GraphicsLayer();
      
      var map = new Map({
        basemap: "topo",
        layers:  [parcelsMapService, resultsLayer]
      });


      var view = new MapView({
        container: "viewDiv",
        map: map,
        zoom: 11,
        center: [-122, 47.5]
      });
      
      var popupTemplate = PopupTemplate({
        title: "Selected Parcel",
        content: "{PIN}"
      });
      
      var queryTask = new QueryTask({
        url: "http://gismaps.kingcounty.gov/arcgis/rest/services/Property/KingCo_PropertyInfo/MapServer/2"
      });


      var pinQuery = new Query({
        returnGeometry: true,
        outFields: ["*"],
        outSpatialReference: {"wkid": 4326}
      });
      
      pinQuery.where = "PIN = '7666204876'";
      
      queryTask.execute(pinQuery)
        .then(getResults)
        .then(addToLayer)
        .then(goToLayer);
        
      function getResults(response) {
        var parcelResults = arrayUtils.map(response.features, function(feature) {
          feature.symbol = new SimpleFillSymbol({
            color: [255, 255, 0, 0.25],
            style: "solid",
            outline: {
              color: [255, 255, 0],
              width: 3
            }
          });
          feature.popupTemplate = popupTemplate;
          return feature;
        });
        return parcelResults;
      }
      
      function addToLayer(parcelResults) {
        resultsLayer.addMany(parcelResults);
      }


      function goToLayer() {
        view.goTo(resultsLayer.graphics);
      }
      
    });
0 Kudos
7 Replies
RobertScheitlin__GISP
MVP Emeritus

Ryan,

  Have you run a geometry check in ArcToolbox on your data?

0 Kudos
RyanKelso
Occasional Contributor III

I haven't Robert, my data is in an SDE geodatabase (which I should have mentioned!)  Thanks for the suggestion.

0 Kudos
KenBuja
MVP Esteemed Contributor

Is there a reason to set the spatial reference of the query to 4326?

0 Kudos
RyanKelso
Occasional Contributor III

Ken Buja,

I could not get this to work until I tried setting the query spatial reference to 4326.  You know, I can't remember why I used 4326 when my intention was probably to use 102100, but it works either way.  Maybe an important piece of information that I forgot to mention is that my own map service data is in State Plane.

I'm just confused that I can sit here hitting refresh on the page and get the error maybe 50% of the time and have it work fine the rest of the time.

0 Kudos
KenBuja
MVP Esteemed Contributor

If it's an error that doesn't always happen, it sounds like the query isn't always returning results. Have you tried putting in any checking mechanism to see if there are indeed results coming back?

RyanKelso
Occasional Contributor III

That does sound like a reasonable inference, Ken.  The only "checking mechanism" I've used (again, pointing out my beginner-ness) is logging information about the query results to the console and checking the response in Chrome's dev tools.  It's a really simple query that I'm testing with a static value in the script and I haven't seen it fail to return results with my simplistic checks.  In fact, even when view.goTo(resultsLayer.graphics) fails and I get the TypeError, I can still see that the geometry from the query results has been added to the map.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Ryan,

  It sounds like your map service may be failing then. Have you check your ArcGIS Server logs to see if there is an issue logged there?

0 Kudos