定义locator标签
<esri:Locator id="locator" url="http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer" locationToAddressComplete="onLocationToAddressComplete(event)" fault="myService_faultHandler(event)" concurrency="last" />
给点添加鼠标事件
//给点添加鼠标事件,用于显示其定位时间 grahpic.addEventListener(MouseEvent.ROLL_OVER,onMapClick); grahpic.addEventListener(MouseEvent.ROLL_OUT,maouseOutHandler);
调用坐标转换地址的服务
private function onMapClick():void{ var myp:MapPoint = new MapPoint(114.242593,30.631004,new SpatialReference(4326)); var wm:MapPoint = WebMercatorUtil.geographicToWebMercator(myp) as MapPoint; locator.locationToAddress(wm,100); }
调用成功后的处理函数,与标签中的locationToAddressComplete属性配置的函数一致
private function onLocationToAddressComplete(event:LocatorEvent):void{ var candidate:AddressCandidate = event.addressCandidate; if (candidate != null && candidate.address){ WebMercatorUtil.geographicToWebMercator(candidate.location) var g:Graphic = new Graphic(candidate.location, sps, candidate.address); EsriGraphicsLayer.clear(); EsriGraphicsLayer.add(g); var text:TextArea = new TextArea(); text.htmlText = "<b>地址:</b>" + candidate.address.Address.toString(); //+ "," + candidate.address.State.toString() EsriMap.infoWindow.content = text; EsriMap.infoWindow.show(g.geometry as MapPoint); } }
失败后的回调函数
//回调失败时调用的函数 protected function myService_faultHandler(event:FaultEvent):void{ Alert.show(event.fault.message); }