PageRenderTime 16ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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