function projectToLatLong(evt) { gsvc = new esri.tasks.GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"); var outSR = new esri.SpatialReference({ wkid: 4326 }); pt = evt.mapPoint; gsvc.project([pt], outSR, function (projectedPoints) { return projectedPoints[0]; }, function (e) { console.log(e.message); }); }
sholyoak, As Derek tries to point out, the call to 'project' is asynchronous which means your inline function that handles the projected geometries will not runs until after you get a reply from the server. On the other hand, the code right after your call to 'project' runs immediately after making the call, which means you're trying to use a variable (newPt) that hasn't been set yet. Try this: function openStreetView(evt) { //convert the point //make sure we have a geometry server if (gsvc === null) initializeProjection(); var outSR = new esri.SpatialReference({ wkid: 4326 }); pt = evt.mapPoint; gsvc.project([pt], outSR, function (projectedPoints) { newPt = projectedPoints[0]; // WAIT FOR SERVER RESPONSE AND THEN DO SOMETHING WITH newPt HERE... //This gives undesirable behavior as the window.open() call results in inconsistent behavior }, function (e) { console.log(e.message); }); // CODE HERE RUNS IMMEDIATELY AFTER CALLING PROJECT BUT RESPONSE FROM SERVER MAY NOT HAVE HAPPENED YET... //The window.open() call works as expected, but the newPt variable is null, so this doesn't workl }
function openStreetView(evt) { //convert the point //make sure we have a geometry server if (gsvc === null) initializeProjection(); var outSR = new esri.SpatialReference({ wkid: 4326 }); pt = evt.mapPoint; gsvc.project([pt], outSR, function (projectedPoints) { newPt = projectedPoints[0]; // WAIT FOR SERVER RESPONSE AND THEN DO SOMETHING WITH newPt HERE... //This gives undesirable behavior as the window.open() call results in inconsistent behavior }, function (e) { console.log(e.message); }); // CODE HERE RUNS IMMEDIATELY AFTER CALLING PROJECT BUT RESPONSE FROM SERVER MAY NOT HAVE HAPPENED YET... //The window.open() call works as expected, but the newPt variable is null, so this doesn't workl }
function openStreetView(evt) { //convert the point //make sure we have a geometry server if (gsvc === null) initializeProjection(); var outSR = new esri.SpatialReference({ wkid: 4326 }); pt = evt.mapPoint; gsvc.project([pt], outSR, function (projectedPoints) { newPt = projectedPoints[0]; // WAIT FOR SERVER RESPONSE AND THEN DO SOMETHING WITH newPt HERE... }, function (e) { console.log(e.message); }); // CODE HERE RUNS IMMEDIATELY AFTER CALLING PROJECT BUT RESPONSE FROM SERVER MAY NOT HAVE HAPPENED YET... }
function openStreetView(evt) { //convert the point //make sure we have a geometry server if (gsvc === null) initializeProjection(); var outSR = new esri.SpatialReference({ wkid: 4326 }); pt = evt.mapPoint; gsvc.project([pt], outSR, function (projectedPoints) { newPt = projectedPoints[0]; }, function (e) { console.log(e.message); }); //create the URL var svurl = "http://maps.google.com/maps?ll=" + newPt.y + "," + newPt.x + "&t=w&z=19&layer=c&cbll=" + newPt.y + "," + newPt.x + "&cbp=13,-85.37630595889367,,0,2.062648062470899"; //open the window window.open(svurl, "_blank"); }
function projectToLatLong(evt) { gsvc = new esri.tasks.GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"); var outSR = new esri.SpatialReference({ wkid: 4326 }); pt = evt.mapPoint; gsvc.project([pt], outSR, function (projectedPoints) { newPt = projectedPoints[0]; }, function (e) { console.log(e.message); }); }
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.