百度地图API之根据经纬度查询地址信息(Android)


本文主要讲解如何通过百度地图API根据某个经纬度值(地理坐标)查询对应的地址信息以及该地址周边的POI(Point of Interest,兴趣点)信息。
      百度地图移动版API不仅包含构建地图的基本接口,还集成了众多搜索服务,包括:位置检索、周边检索、范围检索、公交检索、驾乘检索、步行检索、地址信息查询等。
      百度地图移动版API提供的搜索服务主要是通过初始化MKSearch类,注册搜索结果的监听对象MKSearchListener来实现异步搜索服务。首先需要自定义一个MySearchListener类,它实现MKSearchListener接口,然后通过实现接口中不同的回调方法,来获得对应的搜索结果。MySearchListener类的定义如下:      
  1. /** 
  2.  * 实现MKSearchListener接口,用于实现异步搜索服务,得到搜索结果 
  3.  *  
  4.  * @author liufeng 
  5.  */  
  6. public class MySearchListener implements MKSearchListener {  
  7.     /** 
  8.      * 根据经纬度搜索地址信息结果 
  9.      * @param result 搜索结果 
  10.      * @param iError 错误号(0表示正确返回) 
  11.      */  
  12.     @Override  
  13.     public void onGetAddrResult(MKAddrInfo result, int iError) {  
  14.     }  
  15.   
  16.     /** 
  17.      * 驾车路线搜索结果 
  18.      * @param result 搜索结果 
  19.      * @param iError 错误号(0表示正确返回) 
  20.      */  
  21.     @Override  
  22.     public void onGetDrivingRouteResult(MKDrivingRouteResult result, int iError) {  
  23.     }  
  24.   
  25.     /** 
  26.      * POI搜索结果(范围检索、城市POI检索、周边检索) 
  27.      * @param result 搜索结果 
  28.      * @param type 返回结果类型(11,12,21:poi列表 7:城市列表) 
  29.      * @param iError 错误号(0表示正确返回) 
  30.      */  
  31.     @Override  
  32.     public void onGetPoiResult(MKPoiResult result, int type, int iError) {  
  33.     }  
  34.   
  35.     /** 
  36.      * 公交换乘路线搜索结果 
  37.      * @param result 搜索结果 
  38.      * @param iError 错误号(0表示正确返回) 
  39.      */  
  40.     @Override  
  41.     public void onGetTransitRouteResult(MKTransitRouteResult result, int iError) {  
  42.     }  
  43.   
  44.     /** 
  45.      * 步行路线搜索结果 
  46.      * @param result 搜索结果 
  47.      * @param iError 错误号(0表示正确返回) 
  48.      */  
  49.     @Override  
  50.     public void onGetWalkingRouteResult(MKWalkingRouteResult result, int iError) {  
  51.     }  
  52. }  
说明:上面的类定义只是在说明MKSearchListener类的5个方法的作用,全都是空实现,并未给出具体的实现。根据你要检索的内容,再去具体实现上面对应的方法,就能获取到搜索结果。例如:1)你想通过一个地理坐标(经纬度值)来搜索地址信息,那么只需要具体实现上面的onGetAddrResult()方法就能得到搜索结果;2)如果你想搜索驾车路线信息,只需要具体实现onGetDrivingRouteResult()方法就能得到搜索结果。

紧接着,需要初始化MKSearch类:      

  1. // 初始化MKSearch   
  2. mMKSearch = new MKSearch();  
  3. mMKSearch.init(mapManager, new MySearchListener());  

经过上面两步之后,就可以通过调用MKSearch所提供的一些检索方法来搜索你想要的信息了。
      下面给出一个具体的示例:根据某个经纬度值(地理坐标)查询对应的地址信息以及该地址周边的POI(Point of Interest,兴趣点)信息。
1)布局文件res/layout/query_address.xml       
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <LinearLayout   
  6.         android:orientation="vertical"  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent"  
  9.         >  
  10.         <TextView  
  11.             android:layout_width="fill_parent"  
  12.             android:layout_height="wrap_content"  
  13.             android:text="经度:"  
  14.         />  
  15.         <EditText android:id="@+id/longitude_input"  
  16.             android:layout_width="fill_parent"  
  17.             android:layout_height="wrap_content"   
  18.             android:text="106.720397"  
  19.         />  
  20.           
  21.         <TextView  
  22.             android:layout_width="fill_parent"  
  23.             android:layout_height="wrap_content"  
  24.             android:text="纬度:"  
  25.         />  
  26.         <EditText android:id="@+id/latitude_input"  
  27.             android:layout_width="fill_parent"  
  28.             android:layout_height="wrap_content"   
  29.             android:text="26.597239"  
  30.         />  
  31.       
  32.         <Button android:id="@+id/query_button"  
  33.             android:layout_width="wrap_content"  
  34.             android:layout_height="wrap_content"  
  35.             android:layout_gravity="right"  
  36.             android:text="地址查询"  
  37.         />  
  38.           
  39.         <TextView android:id="@+id/address_text"  
  40.             android:layout_width="wrap_content"  
  41.             android:layout_height="wrap_content"  
  42.         />  
  43.         <!--   
  44.             虽然定义了MapView,但是设置了android:visibility="gone"将其隐藏  
  45.             因为本示例并不需要显示地图,但不定义又不行(baidu map api的要求)   
  46.         -->  
  47.         <com.baidu.mapapi.MapView android:id="@+id/map_View"  
  48.             android:layout_width="fill_parent"  
  49.             android:layout_height="fill_parent"  
  50.             android:clickable="true"  
  51.             android:visibility="gone"  
  52.         />  
  53.     </LinearLayout>  
  54. </ScrollView>  
  • 1
  • 2
  • 下一页

相关内容