AnsweredAssumed Answered

Having a problem with goTo

Question asked by rkelso on May 20, 2016
Latest reply on May 24, 2016 by rscheitlin

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);
      }
      
    });

Outcomes