Select to view content in your preferred language

Find Task not working

1029
3
07-13-2012 07:25 AM
JessicaKnight1
Occasional Contributor
I am a novice at JS, but I have done well with Python scripting in the past. I currently have an application set up in ArcServer but am migrating it over to the JavaScript API for more flexibility. I can't seem to get things to work though. I made use of the "Find" tool example script but things just don't seem to be connecting somewhere. I've had someone else look over the code and they are also stumped. I figured this group here would be a good place to ask for some help. I'm really hoping to get over this hurdle so I can get this online. Everything is looking good, just not working!

I've gotten rid of error codes and things still don't seem to work. The Find Task works fine via REST services using the search term I'm using and it shows results. But it doesn't seem to want to do the same in my actual code. I had the Query Task working previously, but wanted to be able to search multiple layers so I switched to the Find Task.

I will admit I'm not the best script writer yet...again, still pretty new at this. But if I could get some insight on why things don't seem to be working, that would be great. Thanks!
0 Kudos
3 Replies
derekswingley1
Deactivated User
Everything is working as expected, but the map service you're querying is in geographic coordinates (lat, long). The find task is returning data, and it's being added to the map. The issue is that your points are in lat, long while the map is in web mercator. This causes your points to be plotted off the coast of Africa:
[ATTACH=CONFIG]16077[/ATTACH]

The quickest fix for this is to use esri.geometry.geographicToWebMercator to convert your geographic coordinates to web mercator. Here's a modified version of your callback function that handles the find task results:
function showResults(results){
 var markerSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 10, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 0, 0]), 1), new dojo.Color([0, 255, 0, 0.25]));
 map.graphics.clear();
 dojo.forEach(results, function(result){
  result.feature.geometry = esri.geometry.geographicToWebMercator(result.feature.geometry);
  var graphic = result.feature;
  graphic.setSymbol(markerSymbol);
  map.graphics.add(graphic);
 }); 
}
0 Kudos
JessicaKnight1
Occasional Contributor
Thank you so much! I've been fighting with this for weeks and I never would have thought to zoom out.

Any way to make that code universal for other tasks I will add in later (a GP task for example) or do I have to use this code each time I show results for my tasks?
0 Kudos
derekswingley1
Deactivated User

Any way to make that code universal for other tasks I will add in later (a GP task for example) or do I have to use this code each time I show results for my tasks?


A better way to do this would be to set outSpatialReference on your findParams to match your map's spatial reference. For instance: 
findParams = new esri.tasks.FindParameters();
findParams.returnGeometry = true;
findParams.layerIds = [9, 10, 11, 12, 13, 14, 15, 16, 17, 18];
findParams.searchFields = ["FARM_NAME", "LF_NAME", "COMPANY", "FACILITY", "NAME", "Company"];
findParams.outSpatialReference = map.spatialReference;


That way you don't need to do anything other than add the results to your map in your callback:
//Display results for Find tool
function showResults(results){
 var markerSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 10, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255, 0, 0]), 1), new dojo.Color([0, 255, 0, 0.25]));
 map.graphics.clear();
 dojo.forEach(results, function(result){
  var graphic = result.feature;
  graphic.setSymbol(markerSymbol);
  map.graphics.add(graphic);
 }); 
}
0 Kudos