select feature based on geocoder results problem

2187
6
04-24-2014 07:25 AM
MikeSteven
New Contributor III
Hi,

I'm trying to take the geometry of a geocoder search result and use that as the input geometry to query against a feature layer. If the geometry returned by the geocoder search result intersects a polygon in the feature layer, then a popup appears to display some of the attributes of the layer.

This works fine for one-off address searches. It also works fine if I search for a new address after finding the first address and the new address is in a different polygon in the feature layer to the previous polygon that was selected in the first address search. However in the scenario where the first search is completed and a second address is searched and that second address intersects the same polygon as was selected by the first search, I'm having some problems.

In this scenario the map first starts to recentre on the second address but then quickly zooms back to the location of the first address. In the console the following error messages appear:

"exception in animation handler for: onAnimate

TypeError: d is undefined
.cache["esri/dijit/Popup"]/</e<._onPan()/3.8compact/ (line 543)
.cache["dojo/_base/lang"]/</g.hitch/<()/3.8compact/ (line 173)
.cache["dojo/aspect"]/</n/</h()/3.8compact/ (line 238)
.cache["esri/_coremap"]/</p<._panningHandler()/3.8compact/ (line 828)
.cache["dojo/_base/lang"]/</g.hitch/<()/3.8compact/ (line 173)
.cache["dojo/aspect"]/</n/</h.around.advice()/3.8compact/ (line 238)
.cache["dojo/aspect"]/</n/</h()/3.8compact/ (line 238)
.cache["dojo/_base/fx"]/</<._fire()/3.8compact/ (line 44)
.cache["dojo/_base/fx"]/</<._cycle()/3.8compact/ (line 48)
.cache["dojo/_base/lang"]/</g.hitch/<()/3.8compact/ (line 172)
.cache["dojo/aspect"]/</n/</h()/3.8compact/ (line 238)
.cache["dojo/_base/lang"]/</g.hitch/<()/3.8compact/ (line 172)


Reload the page to get source for: http://js.arcgis.com/3.8compact/"

These error messages appear 4 times in a row.

Here is my code:

     function startGeocoder() {
      geocoder = new Geocoder({
       map : map,
       arcgisGeocoder : {
        placeholder : "Address search",
        sourceCountry : "GBR"
       }
      }, "search");
      
      geocoder.autoComplete = true;
      geocoder.startup();
      geocoder.on("select", queryLayer);      
     }
     
     function queryLayer(evt){
      
      popup.hide();
      selectTariffLayer.clearSelection();
      var point = evt.result.feature.geometry;

      var query = Query();
      query.geometry = point;
      selectTariffLayer.selectFeatures(query, selectTariffLayer.SELECTION_NEW, function(results){
       var charge = results[0].attributes["Charge"];
       var price = results[0].attributes["Cost"];

                popup.setTitle("Parking tariffs");
                popup.setContent(charge + "<br/>" + price); 
                popup.show(point);      

      });     
       
      
     }


Any suggestions where I'm going wrong?

Cheers,
Mike
0 Kudos
6 Replies
JonathanUihlein
Esri Regular Contributor
I don't see anything immediately wrong with your code.

Could you recreate the issue using jsfiddle.net so I can take a closer look?

Thanks!
0 Kudos
MikeSteven
New Contributor III

Hi, really sorry for the delay in getting back to you, I got pulled onto another project. Yeah, here it is:

Edit fiddle - JSFiddlehttp://jsfiddle.net/nPqS8/

There seems to be two problems.

1. If searching for addresses in the same polygon, sometimes after searching for a second consecutive address in the same polygon, the map starts to zoom to the second address and then the zoom jumps back to the first address.

2. If searching for addresses in the same polygon in the feature layer that is being queried, sometimes after searching for a new address in the same polygon, the extent changes to centre on the new address but the popup containing the information about that polygon remains displayed over the old address.

Here are some example addresses that should highlight the problems:

  • 17 home street
  • 17 brougham place
  • 1 lauriston gardens
  • 94 fountainbridge

These problems don't seem to occur if searching for addresses that fall into different polygons from the previously selected polygon.

Cheers

0 Kudos
TracySchloss
Frequent Contributor

I don't know if this would be a solution for you, but when I have problems with anything that generates output results, like featureLayer.selectFeatures, it helps me to separate it out to an event listener and an resultsHandler.  You could try adding a selection-complete handler on your featureLayer and move your function to a separate resultsHandler that would execute only when your selection is complete.  Sometimes the problem is the results handler is running before the execute is really fully finished.

The frustrating thing too is that if you add breakpoints it sometimes works!  It can be because the pause of a breakpoint was just enough time for the query to finish.

0 Kudos
RobertWinterbottom
Occasional Contributor

Hmmm, if you comment out popup.show(point) the error goes away, seems like the error is coming from the calling popup.show so it could be something funky with the point your passing to it although the json for that looks fine. 

0 Kudos
RobertWinterbottom
Occasional Contributor

I think I have a solution for you.  Try out this fiddle and see if it works, I was able to get it to not show the onAnimateEnd errors.  I took the popup show out of the callback you had passed to selectFeatures and set the response as a deferred which you then pass into the popup via popup.setFeatures([def]);  I then added a popup.clearFeatures() to the beginning of the queryLayer function to make sure there are no previously selected features there.  Here is the fiddle url and the relevant code is below.  http://jsfiddle.net/nPqS8/4/


function queryLayer(evt){





  popup.hide();


  popup.clearFeatures();


  selectTariffLayer.clearSelection();


  var point = evt.result.feature.geometry;








  var query = Query();


  query.geometry = point;


  var def = selectTariffLayer.selectFeatures(query, selectTariffLayer.SELECTION_NEW, function(results){


       var charge = results[0].attributes["Charge"];


       var price = results[0].attributes["Cost"];                 


       popup.setTitle("Parking tariffs");


       popup.setContent(charge + "<br/>" + price);


  });


  popup.setFeatures([def]);


  popup.show(point);


}






0 Kudos
MikeSteven
New Contributor III

Thanks very much Robert for the fix and thanks Tracey for the suggestions. Yes Robert, it does work just fine.

That's great both of you, really appreciated.

Cheers,

Mike

0 Kudos