In a different post I figured out how to use the Geometry Server to project a point, see:
http://forums.arcgis.com/threads/14793-UTM-or-State-Plane-to-Decimal-Degrees
However, I'm still left with a question. My question is there anyway to avoid adding the Declarations section at the end (i.e. just define the geometryService info in the body of the ActionScript function)??
This is what I'm currently adding:
Thanks
var geomServ:GeometryService = new GeometryService("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"); geomServ.addEventListener(GeometryServiceEvent.PROJECT_COMPLETE,pResult); function pResult(event:GeometryServiceEvent):void{ var pt:MapPoint = (event.result as Array)[0]as MapPoint; map.centerAt(pt); map.cursorManager.removeBusyCursor(); }
private function mouseClickHandler(event:MouseEvent):void { const frmapPoint:MapPoint = map.toMapFromStage(event.stageX, event.stageY); var latlong:MapPoint; var wkidString:String = frmapPoint.spatialReference.wkid.toString(); //Alert.show("Spatial Ref for map: " + frmapPoint.spatialReference.wkid.toString()); if ( wkidString == "102100") { latlong = WebMercatorUtil.webMercatorToGeographic(frmapPoint) as MapPoint; frLat = latlong.y.toFixed(6); frLong = latlong.x.toFixed(6); displayForm(frLat, frLong); } else if ( wkidString == "3857") { latlong = WebMercatorUtil.webMercatorToGeographic(frmapPoint) as MapPoint; frLat = latlong.y.toFixed(6); frLong = latlong.x.toFixed(6); displayForm(frLat, frLong); } else if ( wkidString == "4326") { latlong = map.toMapFromStage(event.stageX, event.stageY); frLat = latlong.y.toFixed(6); frLong = latlong.x.toFixed(6); displayForm(frLat, frLong); } else { //Project using geometry service var outSR:SpatialReference = new SpatialReference(4326); // var inSR:SpatialReference = new SpatialReference(102100); //geometryService.project([ frmapPoint as Geometry ], outSR); // Changed for 3.0 API const projectParameters:ProjectParameters = new ProjectParameters; projectParameters.geometries = [ frmapPoint as Geometry ]; projectParameters.outSpatialReference = outSR; geometryService.project(projectParameters); } var graphic:Graphic = new Graphic(); var mapPoint2:MapPoint = frmapPoint; mapPoint2.spatialReference = map.spatialReference; graphic.geometry = mapPoint2; graphicsLayer.clear(); graphicsLayer.add(graphic); } private function projectCompleteHandler(event:GeometryServiceEvent):void { try { // Note: As of version 2.0, GeometryService returns geometries (instead of graphics) var pt:MapPoint = (event.result as Array)[0]as MapPoint; //Alert.show(pt.x + ", " + pt.y); displayForm(pt.y.toString(),pt.x.toString()); } catch (error:Error) { Alert.show(error.toString()); } }
public function convertToPoint(x:Number, y:Number, bool:Boolean):Point { var _pointObject:MapPoint = new MapPoint(); _pointObject.x = x; _pointObject.y = y; if(bool) { _pointObject = WebMercatorUtil.geographicToWebMercator(_pointObject) as MapPoint; } else { _pointObject = WebMercatorUtil.webMercatorToGeographic(_pointObject) as MapPoint; } return new Point(_pointObject.x, _pointObject.y); }
public function convertToPoint(x:Number, y:Number, bool:Boolean):Point { var _pointObject:MapPoint = new MapPoint(); _pointObject.x = x; _pointObject.y = y; if(bool) { //_pointObject = WebMercatorUtil.geographicToWebMercator(_pointObject) as MapPoint; var geomServ:GeometryService = new GeometryService("http://myserver/arcgis/rest/services/Utilities/Geometry/GeometryServer"); geomServ.addEventListener(GeometryServiceEvent.PROJECT_COMPLETE,pResult); const projParams:ProjectParameters = new ProjectParameters(); projParams.geometries = [_pointObject as MapPoint ]; var outSR:SpatialReference = new SpatialReference(2277); projParams.outSpatialReference = outSR; geomServ.project(projParams); function pResult(event:GeometryServiceEvent):void{ var pt:MapPoint = (event.result as Array)[0]as MapPoint; _pointObject.x = pt.x; _pointObject.y = pt.y; }The code will work up to the geomServ.project(projParams) function, but the project comple function (pResult) will not fire up to complete the projection. I believe that there is an inherent issue with event listeners in action script, and I may be out of luck: I just wanted to bounce this off more experienced programmers. As I implied in my first post, it would be fantastic if we had a way to do something like this since many folks are either stuck with using state plane, or choose to for other reasons. By the time this data gets back to the mxml, it is buried in a complex graphic provider of a graphics layer, making re-projection in mxml a very difficult or unlikely prospect. The idea is to get to this data before it gets loaded into that graphicprovider.
geomServ.project(projParams, new AsyncResponder( function pResult(item:Object, token:Object = null):void{ var pt:MapPoint = (item as Array)[0] as MapPoint; _pointObject.x = pt.x; _pointObject.y = pt.y; }, function pFault(fault:Fault, token:Object = null):void{ Alert.show("Error: " + fault.faultString, "Error code: " + fault.faultCode); } ) );
public function convertToPoint(x:Number, y:Number, bool:Boolean):Point { var _pointObject:MapPoint = new MapPoint(); _pointObject.x = x; _pointObject.y = y; if((bool) && (!isNaN(x)) && (!isNaN(y))) { //_pointObject = WebMercatorUtil.geographicToWebMercator(_pointObject) as MapPoint; var geomServ:GeometryService = new GeometryService("https://my-imap.austinisd.org/arcgis/rest/services/Utilities/Geometry/GeometryServer"); //geomServ.addEventListener(GeometryServiceEvent.PROJECT_COMPLETE,pResult); const projParams:ProjectParameters = new ProjectParameters(); projParams.geometries = [_pointObject as MapPoint ]; var outSR:SpatialReference = new SpatialReference(2277); projParams.outSpatialReference = outSR; geomServ.project(projParams, new AsyncResponder( function pResult(item:Object, token:Object = null):void{ var pt:MapPoint = (item as Array)[0] as MapPoint; _pointObject.x = pt.x; _pointObject.y = pt.y; }, function pFault(fault:Fault, token:Object = null):void{ Alert.show("Error: " + fault.faultString, "Error code: " + fault.faultCode); } ) ); }The url that forms (from debug) is
http://mapaisd/arcgis/rest/services/Utilities/Geometry/GeometryServer/project?outSR=2277&f=json&geometries=%7B%22geometryType%22%3A%22esriGeometryPoint%22%2C%22geometries%22%3A%5B%7B%22y%22%3A30%2E49213059%2C%22x%22%3A%2D97%2E64269802%7D%5D%7D