Geometry Server projection error

1312
9
04-10-2012 10:48 AM
ShawnHolyoak
Occasional Contributor
It seems really simple, yet I keep getting an error when trying to project a point. 

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); });
}


pt is a valid esri.geometry.Point, in the 2276 spatial reference.  The error I get is "projectedPoints is not defined".  I've tried it with my own internal geometry service, and ESRI's, and I always get the same error.  I've tried using the web based REST api for each of those two services, and both project the geometry properly.  My error callback doesn't write anything to the console either.  What's going on?  Thanks.
0 Kudos
9 Replies
derekswingley1
Frequent Contributor II
Why are you using return instead of directly doing something with your projectedPoints array?
0 Kudos
ShawnHolyoak
Occasional Contributor
I need to construct a URL to open a new window.  I suppose I could do it within the function, but I don't think that will make any difference, as I don't even get into the function.
0 Kudos
ShawnHolyoak
Occasional Contributor
Apparently it is the return.  I altered it slightly to set a global variable to projectedPoints[0], and then used that global to extract the xy to use in constructing the url.  I have it working now.  Thanks for pointing me in the right direction.

Final code:
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); });
}
0 Kudos
ShawnHolyoak
Occasional Contributor
OK, I've got it close, but the deferred aspect is messing things up.  What I'm trying to do, big picture, is open Google street view at the location the user clicks.  The problem is now, that the deferred finishes after the new window is opened with an invalid url.  The second time the user clicks, the url is now valid (but with the location of the first click!) and street view opens fine.  If I put svurl and the window.open call inside the deferred, the url is constructed properly, but the window now opens in a new window (it's treated as a pop-up by webkit, and I have to specifically allow my site to open pop-ups), not a new tab, whereas if I put the window.open call outside of the deferred, it opens in a new tab, and is NOT treated as a pop-up.  Is there a better way?

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");
}
0 Kudos
EdSaunders
Occasional Contributor
Shawn, not sure if you're still working on this.  I implemented Dual Maps in my app for this functionality.  Obviously the code is handled by Dual Maps so there's no customising it but it's a quick way to get StreetView into an AGS JavaScript app.
0 Kudos
ShawnHolyoak
Occasional Contributor
I wasn't aware of Dual Maps.  I'll take a good look at it.  Thanks for the tip.
0 Kudos
JohnGrayson
Esri Regular Contributor
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...

    }, function (e) { console.log(e.message); });

    // CODE HERE RUNS IMMEDIATELY AFTER CALLING PROJECT BUT RESPONSE FROM SERVER MAY NOT HAVE HAPPENED YET...

}
0 Kudos
ShawnHolyoak
Occasional Contributor
Right. As I mentioned in my post on April 10, I figured out that was the underlying problem. The problem now is, as I explained in that post, by opening the window within the callback once I get the response from the server, I get inconsistent and undesired behavior from the call. I don't understand why opening the window from the callback results in different behavior than opening it directly as a result of the click event.

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

}
0 Kudos
nicogis
MVP Frequent Contributor
I am not answering to your question but cannot you create a dialog dojo ( http://dojotoolkit.org/reference-guide/1.7/dijit/Dialog.html ) and in it you can use for example an iframe with src to a page html where you pass only lat,lon for view in street view?
0 Kudos