地理编码与地理反编码

地理编码 (Geocoding)与地理反编码 (Reverse Geocoding)是地图操作中的常见操作,前者表示通过街道地址请求空间坐标,后者表示通过空间坐标请求街道地址。通俗的说,二者就是街道地址与经纬 度的转换。举例来说,前者就是输入查询"上海市杨浦区四平路1239号"得到(31.285207060526762, 121.50546412914991),而后者则表示这个反过程。

在实际的移动开发过程中,地图相关的操作对于地理编码与地理反编码的使用都是十分普遍。幸运的是,Android的MapView控件中对于这两者 都进行了封装,因此可以方便的利用Google Map Service进行二者查询。下面将对开发过程做一个简单介绍。

首先必须进行MapKey的申请,任何地图的显示都需要申请一个MapKey。具体的申请步骤可见

http://code.google.com/intl/zh-CN/android/maps-api-signup.html

然后可以建立一个基于Google APIs的程序,并且在AndroidManifest.xml中加入地图API的支持。

  1. <? xml   version = "1.0"   encoding = "utf-8" ?>   
  2. < manifest   xmlns:android = "http://schemas.android.com/apk/res/android"   
  3.       package = "net.learn2develop.GoogleMaps"   
  4.       android:versionCode = "1"   
  5.       android:versionName = "1.0.0" >   
  6.     < application   android:icon = "@drawable/icon"   android:label = "@string/app_name" >   
  7.     < uses-library   android:name = "com.google.android.maps"   />     
  8.         < activity   android:name = ".MapsActivity"   
  9.                   android:label = "@string/app_name" >   
  10.             < intent-filter >   
  11.                 < action   android:name = "android.intent.action.MAIN"   />   
  12.                 < category   android:name = "android.intent.category.LAUNCHER"   />   
  13.             </ intent-filter >   
  14.         </ activity >   
  15.     </ application >   
  16.    
  17.     < uses-permission   android:name = "android.permission.INTERNET"   />   
  18. </ manifest >   
  19. </ xml >   

 

接着可以在主Layout文件中加入对于地图的显示,这里需要加入刚才申请的MapKey,否则地图将无法正常显示。

  1. <? xml   version = "1.0"   encoding = "utf-8" ?>   
  2. < RelativeLayout   xmlns:android = "http://schemas.android.com/apk/res/android"    
  3.     android:layout_width = "fill_parent"    
  4.     android:layout_height = "fill_parent" >   
  5.    
  6.     < com.google.android.maps.MapView    
  7.         android:id = "@+id/mapView"   
  8.         android:layout_width = "fill_parent"   
  9.         android:layout_height = "fill_parent"   
  10.         android:enabled = "true"   
  11.         android:clickable = "true"   
  12.         android:apiKey = "MapKey"   
  13.         />   
  14.    
  15. </ RelativeLayout >   

接着在主Activity的JAVA文件进行修改,支持地图显
示。

  1. import  com.google.android.maps.MapActivity;  
  2. import  com.google.android.maps.MapView;  
  3. import  android.os.Bundle;  
  4.    
  5. public   class  MapsActivity  extends  MapActivity   
  6. {      
  7.     /** Called when the activity is first created. */   
  8.     @Override   
  9.     public   void  onCreate(Bundle savedInstanceState)  
  10.     {  
  11.         super .onCreate(savedInstanceState);  
  12.         setContentView(R.layout.main);  
  13.     MapView mapView = (MapView) findViewById(R.id.mapView);  
  14.     mapView.setBuiltInZoomControls(true );  
  15.     }  
  16. }  
 

此时运行程序,地图应该就可以正常显示了,见下图。


此时我们再向程序中加入地理编码与地理反编码的功能,其参考代码如下。 

地理编码:

  1. Geocoder geoCoder =  new  Geocoder( this , Locale.getDefault());      
  2. try  {  
  3.     List<Address> addresses = geoCoder.getFromLocationName(  
  4.         "上海市杨浦区四平路1239号"5 );  
  5.     String add = "" ;  
  6.     if  (addresses.size() >  0 ) {  
  7.         p = new  GeoPoint(  
  8.                 (int ) (addresses.get( 0 ).getLatitude() * 1E6),   
  9.                 (int ) (addresses.get( 0 ).getLongitude() * 1E6));  
  10.         mc.animateTo(p);      
  11.         mapView.invalidate();  
  12.     }      
  13. catch  (IOException e) {  
  14.     e.printStackTrace();  
  15. }  

地理反编码,其中MapOverlay为地图图层上的叠加图层,用于标识的显示以及点击事件的捕捉。

 

  1. class  MapOverlay  extends  com.google.android.maps.Overlay  
  2.     {  
  3.         @Override   
  4.         public   boolean  draw(Canvas canvas, MapView mapView,   
  5.         boolean  shadow,  long  when)   
  6.         {  
  7.           //...   
  8.         }  
  9.    
  10.         @Override   
  11.         public   boolean  onTouchEvent(MotionEvent event, MapView mapView)   
  12.         {     
  13.             //---when user lifts his finger---   
  14.             if  (event.getAction() ==  1 ) {                  
  15.                 GeoPoint p = mapView.getProjection().fromPixels(  
  16.                     (int ) event.getX(),  
  17.                     (int ) event.getY());  
  18.    
  19.                 Geocoder geoCoder = new  Geocoder(  
  20.                     getBaseContext(), Locale.getDefault());  
  21.                 try  {  
  22.                     List<Address> addresses = geoCoder.getFromLocation(  
  23.                         p.getLatitudeE6()  / 1E6,   
  24.                         p.getLongitudeE6() / 1E6, 1 );  
  25.    
  26.                     String add = "" ;  
  27.                     if  (addresses.size() >  0 )   
  28.                     {  
  29.                         for  ( int  i= 0 ; i<addresses.get( 0 ).getMaxAddressLineIndex();   
  30.                              i++)  
  31.                            add += addresses.get(0 ).getAddressLine(i) +  "/n" ;  
  32.                     }  
  33.    
  34.                     Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();  
  35.                 }  
  36.                 catch  (IOException e) {                  
  37.                     e.printStackTrace();  
  38.                 }     
  39.                 return   true ;  
  40.             }  
  41.             else                   
  42.                 return   false ;  
  43.         }          
  44.     }  

 最终实现结果如下图所示,地理编码,查询“上海市杨浦区四平路1239号”,结果其实略有偏差。中国的地址与邮编比较混乱,所以结果有些地方无法 做到完全准确。

地理反编码


本文系“首届 Google 暑期大学生博客分享大赛——2010 Andriod 篇”参赛文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值