Converted point always shows lnfo for last item of array

592
6
Jump to solution
08-28-2019 07:41 AM
ScottWilson2
New Contributor II

I'm using the Geometry Service to  convert a point to wkid 102100.  This works fine and all my points are plotted on the map, but the address in the infoTemplate is wrong, it's always pulling through the last item from my data array.

I've tried creating a counter and passing it to gsvc.project() but due to the asynchronous nature, it's not passing as i'd expect

How do i change my code so that the data in each loop is bound to the response from Geometry Service?  All that is returned in the response is x, y and spacial reference - I loose reference to which item in the array this is for.

  Using API 3.29

var pointInfoTemplate = new InfoTemplate("Hello " + results.features[params.counter].attributes.UFRM_BLPU_POINT_ADDRESS);

var counter = 0;
 
 for (var item in results.features) {
 
 var x = results.features[item].geometry.x;
 var y = results.features[item].geometry.y;
 
 
 var point = new Point(x, y, new SpatialReference({ wkid: 27700 }) );
 
 
 
 var gsvc = new GeometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
 var params = new ProjectParameters();
 params.geometries = [point];
 params.outSR = new SpatialReference({ wkid : 102100 });
 params.counter = counter;
gsvc.project(params, function(ref){
 

 var point2 = new Point(ref[0]);
 var pointSymbol = new SimpleMarkerSymbol();
 var pointAttributes = { city: "123", state: "456" };
 
 
 var pointInfoTemplate = new InfoTemplate("Hello " + results.features[params.counter].attributes.UFRM_BLPU_POINT_ADDRESS); 
var pointGraphic = new Graphic(point2, pointSymbol, pointAttributes).setInfoTemplate(pointInfoTemplate);
 map.graphics.add(pointGraphic);

 });
 
 counter++;
 
 }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Scott,

   map is a standard javaScript function for looping over an array and should not be confused with an esri Map. So based on the info you provided in your last response the "response" object is an array so the code would look like this.

response.map(function(result, index){
});

It would be helpful if you provided answers to my other questions as this whole thread might be taking you down the wrong path based on the answers.

View solution in original post

6 Replies
by Anonymous User
Not applicable

I haven't tried to resolve the issue in your script, but you can make ajax call to geometry service instead of using ArcGIS Javascript API and pass your input to get it back in your response.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Scott,

  I have several questions and concerns about your code.

  1. Your code is doing a for loop on results.features. So is results.features coming from a querytask?
  2. If number one is true then why are you not specifying the queries outSpatialRefernece?
  3. Inside your loop you are creating a new point object with a spatial reference of 27700 is that not already the spatial reference of the graphic inside the results.features array?

Code concerns you are creating a Geometry service inside a loop. So each iteration of the loop you are creating a new GeometryService (bad practice).

Your code should look something more like this:

var gsvc = new GeometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

var geoms = results.features.map(function(gra){
  return gra.geometry;
});

var pointSymbol = new SimpleMarkerSymbol();
var params = new ProjectParameters();
params.geometries = geoms;
params.outSR = new SpatialReference({ wkid : 102100 });
gsvc.project(params, function(response){
  response.features.map(function(result, index){
    var pointAttributes = { city: "123", state: "456" };
//results from a projection are gaurenteed to be in the same order they are sent in.
    var pointInfoTemplate = new InfoTemplate("Hello " + results.features[index].attributes.UFRM_BLPU_POINT_ADDRESS);
    var pointGraphic = new Graphic(result.geometry, pointSymbol, pointAttributes, pointInfoTemplate);
    map.graphics.add(pointGraphic);
  });
});
 
 

ScottWilson2
New Contributor II

Hi Robert, yes, i'm using a Query Task:

// setup CAG query
 var cag = "http://myserver/external/rest/services/CAGAddress/FeatureServer/0?token=" + token;
 var qtCAGsearch = new QueryTask(cag);
 var qCAGsearch = new Query();
 qCAGsearch.outFields = ["*"];
 qCAGsearch.orderByFields = ["BuildingNumber ASC"];
 qCAGsearch.returnGeometry = true;

I've tried your code but i'm getting an error: Cannot read property 'map' of undefined

After some checks, it's triggered by this line:

response.features.map(function(result, index){
});

I've written the contents of 'response' to the console, it appears to contain point data(?), no map information:

[{"x":-489333.7876013273,"y":7518186.249980049,"spatialReference":{"wkid":102100}}

Sorry, new to this so appreciate the help.

thanks

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Scott,

   map is a standard javaScript function for looping over an array and should not be confused with an esri Map. So based on the info you provided in your last response the "response" object is an array so the code would look like this.

response.map(function(result, index){
});

It would be helpful if you provided answers to my other questions as this whole thread might be taking you down the wrong path based on the answers.

ScottWilson2
New Contributor II

Hi Robert, yes, i was getting confused with the meaning of 'map' in that context

The code was 99% complete.  I've changed 'result.geometry' to just 'result' and it's successfully plotting the points on my map!

var pointGraphic = new Graphic(result.geometry, pointSymbol, pointAttributes, pointInfoTemplate);

var pointGraphic = new Graphic(result, pointSymbol, pointAttributes, pointInfoTemplate);

This is now working perfectly.  My points are being converted and i can access the item data from the feature attributes. 

thanks for all your help.

Regarding point 2.  I don't believe I've set the outSpacialReference.  I shall try this and see what happens!

Scott

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Don't forget to mark this question as answered by clicking on the "Mark Correct" link on the reply that answered your question.

0 Kudos