/src/com/google/maps/extras/arcgislink/GeocodeService.as

http://gmaps-utility-library-flash.googlecode.com/ · ActionScript · 148 lines · 88 code · 25 blank · 35 comment · 11 complexity · ea4c157443bef3d5c191b44c790247be MD5 · raw file

  1. /*
  2. * ArcGIS for Google Maps Flash API
  3. *
  4. * License http://www.apache.org/licenses/LICENSE-2.0
  5. */
  6. /**
  7. * @author nianwei at gmail dot com
  8. */
  9. package com.google.maps.extras.arcgislink {
  10. import flash.events.Event;
  11. import flash.events.EventDispatcher;
  12. import flash.events.IEventDispatcher;
  13. /**
  14. * This class represent an <a href="http://resources.esri.com/help/9.3/arcgisserver/apis/rest/geocodeserver.html">GeocodeServer</a>
  15. * service.
  16. */
  17. public class GeocodeService implements IEventDispatcher {
  18. public var url:String;
  19. public var serviceDescription:String;
  20. public var addressFields:Array;
  21. public var candidateFields:Array;
  22. public var intersectionCandidateFields:Array;
  23. public var spatialReference:SpatialReference;
  24. public var locatorProperties:*;
  25. public var currentVersion:*;
  26. public function GeocodeService(url:String) {
  27. dispatcher_=new EventDispatcher(this);
  28. this.url=url;
  29. var me:GeocodeService=this;
  30. ArcGISUtil.restRequest(url, {f: 'json'}, me, function(json:*):void {
  31. me.init_(json);
  32. });
  33. }
  34. /**
  35. * init
  36. * @param {Object} json
  37. */
  38. private function init_(json:*):void {
  39. if (json.spatialReference){
  40. // some service will miss saptial reference definition like the one on sampleserver1.arcgisonline.com
  41. this.spatialReference= SpatialReferences.getSpatialReference(json.spatialReference.wkid);
  42. } else {
  43. this.spatialReference= SpatialReferences.WGS84;
  44. }
  45. ArcGISUtil.augmentObject(json, this);
  46. /**
  47. * This event is fired when the service and it's service info is loaded.
  48. * @name ArcGISGeocodeService#load
  49. * @event
  50. */
  51. // triggerEvent(this, 'load');
  52. this.dispatchEvent(new ServiceEvent(ServiceEvent.LOAD, this));
  53. }
  54. /**
  55. * If this ArcGISGeocodeService meta data has loaded. useful to get the Spatial Reference information.
  56. * @return {Boolean}
  57. */
  58. public function hasLoaded():Boolean {
  59. return this.addressFields !== null;
  60. }
  61. public function findAddressCandidates(gparams:GeocodeParameters, callbackFn:Function=null, failedFn:Function=null):void {
  62. var params:*=ArcGISUtil.augmentObject(gparams, {});
  63. params.f=params.f || 'json';
  64. if (params.inputs) {
  65. ArcGISUtil.augmentObject(params.inputs, params);
  66. delete params.inputs;
  67. }
  68. if (ArcGISUtil.isArray(params.outFields)) {
  69. params.outFields=params.outFields.join(',');
  70. }
  71. var me:GeocodeService=this;
  72. ArcGISUtil.restRequest(this.url + '/findAddressCandidates', params, this, function(json:*):void {
  73. var res:GeocodeResults=new GeocodeResults(json, me.spatialReference);
  74. if (callbackFn != null) {
  75. callbackFn.call(me, res);
  76. }
  77. me.dispatchEvent(new ServiceEvent(ServiceEvent.GEOCODE_COMPLETE, res));
  78. }, failedFn);
  79. }
  80. public function geocode(gparams:GeocodeParameters, callback:Function=null, failedFn:Function=null):void {
  81. this.findAddressCandidates(gparams, callback, failedFn);
  82. }
  83. /**
  84. * The reverseGeocode operation is The reverseGeocode operation is performed on a geocode service resource.
  85. * The result of this operation is a reverse geocoded address resource.
  86. * param is an instance of {@link ArcGISReverseGeocodeParameters}. An instance of
  87. * {@link ArcGISReverseGeocodeResult} will be passed into callback function.
  88. * @param {ReverseGeocodeParameters} params
  89. * @param {Function} callback
  90. */
  91. public function reverseGeocode(gparams:ReverseGeocodeParameters, callbackFn:Function=null, failedFn:Function=null):void {
  92. var params:*=ArcGISUtil.augmentObject(gparams, {});
  93. params.f=params.f || 'json';
  94. if (!ArcGISUtil.isString(params.location)) {
  95. params.location=ArcGISUtil.fromGeometryToJSON(ArcGISUtil.fromLatLngToPoint(params.location));
  96. }
  97. var me:GeocodeService=this;
  98. ArcGISUtil.restRequest(this.url + '/reverseGeocode', params, this, function(json:*):void {
  99. var res:ReverseGeocodeResult=new ReverseGeocodeResult(json, me.spatialReference);
  100. if (callbackFn != null) {
  101. callbackFn.call(me, res);
  102. }
  103. me.dispatchEvent(new ServiceEvent(ServiceEvent.REVERSEGEOCODE_COMPLETE, res));
  104. }, failedFn);
  105. }
  106. private var dispatcher_:EventDispatcher;
  107. public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void {
  108. dispatcher_.addEventListener(type, listener, useCapture, priority);
  109. }
  110. public function dispatchEvent(evt:Event):Boolean {
  111. return dispatcher_.dispatchEvent(evt);
  112. }
  113. public function hasEventListener(type:String):Boolean {
  114. return dispatcher_.hasEventListener(type);
  115. }
  116. public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void {
  117. dispatcher_.removeEventListener(type, listener, useCapture);
  118. }
  119. public function willTrigger(type:String):Boolean {
  120. return dispatcher_.willTrigger(type);
  121. }
  122. }
  123. }