flex谷歌地图中国各级地区详细信息实例

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/huiyi789/article/details/83808348
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
      xmlns:s="library://ns.adobe.com/flex/spark"
      xmlns:mx="library://ns.adobe.com/flex/halo"
      xmlns:maps="com.google.maps.*"
      fontSize="16" minWidth="1024" minHeight="768">

<fx:Script>
   <![CDATA[
    import com.google.maps.*;
    import com.google.maps.services.ClientGeocoder;
    import com.google.maps.services.GeocodingEvent;
    import com.google.maps.services.GeocodingEvent;
    import com.google.maps.services.ClientGeocoderOptions;
   
    /***********************加载地图***********************************/
    private function onMapReady(event:MapEvent):void{}   //只是初始化地图,确定了以用户为中心的地图
   
   
    /*******************************经纬度为参数*************************/
    public function latlngToAddr(lat:Number, lng:Number):void{    //双击DataGrid 和 点击"查看路线"按钮执行这个函数
     try{
      var geocoder:ClientGeocoder = new ClientGeocoder(new ClientGeocoderOptions({language:'zh', countryCode:'CN'}));
      geocoder.reverseGeocode(new LatLng(lat, lng));
      geocoder.addEventListener(GeocodingEvent.GEOCODING_SUCCESS, latLngToRight);   //经纬度转化为地址名称
      geocoder.addEventListener(GeocodingEvent.GEOCODING_FAILURE, latLngToError);    //经纬度不能转化为地址名称
     }catch(tError:TypeError){
      trace("地图加载失败,请重试");
     }
    }
   
    private function latLngToRight(event:GeocodingEvent):void{
     var markArr:Array = event.response.placemarks;
     if(markArr != null){
      trace(event.response.name);    //35.907000,116.408000
      trace(markArr[0]);    //中国山东泰安东平县 @(35.937102, 116.470304)
      trace(markArr[1]);    //中国山东泰安 @(36.20002, 117.088115)
      trace(markArr[2]);    //中国山东 @(36.669227, 117.019896)
      trace(markArr[3]);    //中国 @(35.86166, 104.195397)
     
      var xianArr:Array = String(markArr[0]).split(" @");    //县级
      trace(xianArr[0]);    //中国山东泰安东平县
      trace(xianArr[1]);    //(35.937102, 116.470304)
     
      var cityArr:Array = String(markArr[1]).split(" @");    //市级
      trace(cityArr[0]);    //中国山东泰安
      trace(cityArr[1]);    //(36.20002, 117.088115)
     
      var provinceArr:Array = String(markArr[2]).split(" @");    //省级
      trace(provinceArr[0]);    //中国山东
      trace(provinceArr[1]);    //(36.669227, 117.019896)
     
      var countryArr:Array = String(markArr[3]).split(" @");    //国家级
      trace(countryArr[0]);    //中国
      trace(countryArr[1]);    //(35.86166, 104.195397)
     }
    }
   
    private function latLngToError(event:GeocodingEvent):void{    //经纬度没有转化成地址位置时
     trace("经纬度没有转化成对应的地址名称");
    }
   
   
    /******************************地址为参数**************************************/
    public function addrToAddr(addr:String):void{
     try{
      var myGeocoder:ClientGeocoder = new ClientGeocoder(new ClientGeocoderOptions({language:'zh', countryCode:'CN'}));
      myGeocoder.addEventListener(GeocodingEvent.GEOCODING_SUCCESS, geocodingUserAddrSuccess);
      myGeocoder.addEventListener(GeocodingEvent.GEOCODING_FAILURE, geocodingUserAddrFailure);
      myGeocoder.geocode(addr);
     }catch(tError:TypeError){
      trace("地图加载失败,请重试!");
     }
    }
   
    private function geocodingUserAddrSuccess(gEvt:GeocodingEvent):void{    //如果搜索当前用户的位置成功
     var markArr:Array = gEvt.response.placemarks;
     if(markArr != null){
      trace(gEvt.response.name);    //三河市
      trace(markArr[0]);    //中国河北廊坊三河市 @(39.982924, 117.078211)
     
      var xianArr:Array = String(markArr[0]).split(" @");    //县级
      trace(xianArr[0]);    //中国河北廊坊三河市
      trace(xianArr[1]);    //(39.982924, 117.078211)
     }
    }
   
    private function geocodingUserAddrFailure(gEvt:GeocodingEvent):void{
     trace("地址名称没有成功转化为对应经纬度");
    }
   
   
   ]]>
</fx:Script>

<maps:Map id="mapID" horizontalCenter="0" y="100" width="80%" height="60%" language="zh-CN" countryCode="gbk"
     url="http://code.google.com/apis/maps/" key="ABQIAAAAEUQL_IqwWX4ZOcEtw0r7whRi_j0U6kJrkFvY4-OX2XYmEAa76BQc0gZPMJROw3owe9BMoRHM0FER_A"/>

<mx:Button id="latlngButID" x="300" label="经纬度" click="latlngToAddr(35.907, 116.408);"/>
<mx:Button id="addrButID" x="400" label="地址" click="addrToAddr('三河市');"/>

</s:Application>
 
展开阅读全文

没有更多推荐了,返回首页