Project extent driving me insane

1745
4
03-04-2013 12:50 PM
MatthewLawton
Occasional Contributor
I used to have a nifty coordinate display handler that worked just fine in pre-3.0. It looked like this:

function updateExtentStatePlaneNevadaWest(updatedExtent) {
 geometryService.project([updatedExtent], new esri.SpatialReference({ wkid: 102709}), function(featVar) {
  newExtent = featVar[0]; 
  //after reprojected, connect to listen to mouse move and drag events
  var onMouseMoveHandler = dojo.connect(map, "onMouseMove", showCoordinates);
  var onMouseDragHandler = dojo.connect(map, "onMouseDrag", showCoordinates);
 });
}


Now I am trying to catch up with the crowd and get my applications on JSAPI version 3.2. This simple extent project is causing me a major headache. Below was my first attempt, but the "outputExtent" variable just wouldn't get populated:

function updateExtentStatePlaneNevadaWest(updatedExtent) {
 var inputExtent = new esri.geometry.Extent(updatedExtent.xmin,updatedExtent.ymin,updatedExtent.xmax,updatedExtent.ymax, new esri.SpatialReference({ wkid: 102113} ));
 var params = new esri.tasks.ProjectParameters();
 params.geometries = [inputExtent];
 params.outSR = new esri.SpatialReference({ wkid: 102709});
 geometryService.project(params, function(outputExtent) {
  newExtent = outputExtent[0]; //<-- comes out as undefined
  //after reprojected, connect to listen to mouse move and drag events
  var onMouseMoveHandler = dojo.connect(map, "onMouseMove", showCoordinates);
  var onMouseDragHandler = dojo.connect(map, "onMouseDrag", showCoordinates);
 });
}


In trying to debug this thing, I have broken down the variables and put the callback into a separate function, but I still can't get it to trigger. I'll admit that I'm probably not deciphering the error message properly. I can get the error callback to trigger, but I can't see what it is telling me. Chrome just gives me this error:

GET http://services.arcgisonline.com/ArcGIS/rest/info?f=json undefined (undefined)


I know there has got to be something simple I am over-looking. Any guidance is much appreciated.
0 Kudos
4 Replies
MatthewLawton
Occasional Contributor
To provide a follow-up... I think I am getting stuck in some sort of JSON formatting problem. If I define the input extent as an extent object, as I have always done, then I get an error about the "toJson" method being unavailable on undefined. So that seems to tell me that perhaps I am not providing the project function a properly formatted extent object.

The code below returns the error "TypeError: Object [object Object] has no method 'toJson':

function updateExtentStatePlaneNevadaWest(updatedExtent) {
 var inputExtent = new esri.geometry.Extent([updatedExtent], new esri.SpatialReference({ wkid: 102113} ));
 gsvc.project([inputExtent], [{"wkid":102709}], function(geometries) {
  newExtent = geometries[0]; //<-- comes out as undefined
  //after reprojected, connect to listen to mouse move and drag events
  var onMouseMoveHandler = dojo.connect(map, "onMouseMove", showCoordinates);
  var onMouseDragHandler = dojo.connect(map, "onMouseDrag", showCoordinates);
 }, onError);
}


If I modify the input projection to conform to the JSON format as below, then I get the same error:

function updateExtentStatePlaneNevadaWest(updatedExtent) {
 var inputExtent = new esri.geometry.Extent({"xmin":updatedExtent.xmin,"ymin":updatedExtent.ymin,"xmax":updatedExtent.xmax,"ymax":updatedExtent.ymax, "spatialReference":{"wkid":102113}});
 gsvc.project([inputExtent], [{"wkid":102709}], function(geometries) {
  newExtent = geometries[0]; //<-- comes out as undefined
  //after reprojected, connect to listen to mouse move and drag events
  var onMouseMoveHandler = dojo.connect(map, "onMouseMove", showCoordinates);
  var onMouseDragHandler = dojo.connect(map, "onMouseDrag", showCoordinates);
 }, onError);
}


If I change the spatial reference output to follow the example in the API reference, then I get the error "Error: JSON syntax error":

function updateExtentStatePlaneNevadaWest(updatedExtent) {
 var inputExtent = new esri.geometry.Extent([updatedExtent], new esri.SpatialReference({ wkid: 102113} ));
 var outSR = new esri.SpatialReference({wkid: 102709});
 gsvc.project(inputExtent, outSR, function(geometries) {
  newExtent = geometries[0]; //<-- comes out as undefined
  //after reprojected, connect to listen to mouse move and drag events
  var onMouseMoveHandler = dojo.connect(map, "onMouseMove", showCoordinates);
  var onMouseDragHandler = dojo.connect(map, "onMouseDrag", showCoordinates);
 }, onError);
}


Very frustrating. I hope somebody can provide me with the magic combination.
0 Kudos
HeathClark
New Contributor III
in case you didn't solve this, or for the next person to come across this thread:

replace
var outSR = new esri.SpatialReference({wkid: 102709});
with
var outSR = new esri.SpatialReference(102709);

works for me, and I solved another issue I was having by looking at your posted code.
DavidPuckett
Occasional Contributor

This helped me 3+ years later. Thanks for posting.

0 Kudos
JimWharton
New Contributor III
I was going the same route, console.log() says it's an object. Since it's coming from JSON, I'd almost expect that it would be a string.

Let me try manually setting the spatial reference and see if that takes care of it.
0 Kudos