当前位置: 代码迷 >> Android >> 百度舆图API之根据经纬度查询地址信息(Android)[转]
  详细解决方案

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

热度:130   发布时间:2016-05-01 20:48:48.0
百度地图API之根据经纬度查询地址信息(Android)[转]

http://blog.csdn.net/lyq8479/article/details/6387860

?

本文主要讲解如何通过百度地图API根据某个经纬度值(地理坐标)查询对应的地址信息以及该地址周边的POI(Point of Interest,兴趣点)信息。
??????百度地图移动版API不仅包含构建地图的基本接口,还集成了众多搜索服务,包括:位置检索、周边检索、范围检索、公交检索、驾乘检索、步行检索、地址信息查询等。
????? 百度地图移动版API提供的搜索服务主要是通过初始化MKSearch类,注册搜索结果的监听对象MKSearchListener来实现异步搜索服务。首先需要自定义一个MySearchListener类,它实现MKSearchListener接口,然后通过实现接口中不同的回调方法,来获得对应的搜索结果。MySearchListener类的定义如下:

[java]?view plaincopy
  1. /**?
  2. ?*?实现MKSearchListener接口,用于实现异步搜索服务,得到搜索结果?
  3. ?*??
  4. [email protected]?
  5. ?*/??
  6. public?class?MySearchListener?implements?MKSearchListener?{??
  7. ????/**?
  8. ?????*?根据经纬度搜索地址信息结果?
  9. [email protected]?
  10. [email protected](0表示正确返回)?
  11. ?????*/??
  12. ????@Override??
  13. ????public?void?onGetAddrResult(MKAddrInfo?result,?int?iError)?{??
  14. ????}??
  15. ??
  16. ????/**?
  17. ?????*?驾车路线搜索结果?
  18. [email protected]?
  19. [email protected](0表示正确返回)?
  20. ?????*/??
  21. ????@Override??
  22. ????public?void?onGetDrivingRouteResult(MKDrivingRouteResult?result,?int?iError)?{??
  23. ????}??
  24. ??
  25. ????/**?
  26. ?????*?POI搜索结果(范围检索、城市POI检索、周边检索)?
  27. [email protected]?
  28. [email protected](11,12,21:poi列表?7:城市列表)?
  29. [email protected](0表示正确返回)?
  30. ?????*/??
  31. ????@Override??
  32. ????public?void?onGetPoiResult(MKPoiResult?result,?int?type,?int?iError)?{??
  33. ????}??
  34. ??
  35. ????/**?
  36. ?????*?公交换乘路线搜索结果?
  37. [email protected]?
  38. [email protected](0表示正确返回)?
  39. ?????*/??
  40. ????@Override??
  41. ????public?void?onGetTransitRouteResult(MKTransitRouteResult?result,?int?iError)?{??
  42. ????}??
  43. ??
  44. ????/**?
  45. ?????*?步行路线搜索结果?
  46. [email protected]?
  47. [email protected](0表示正确返回)?
  48. ?????*/??
  49. ????@Override??
  50. ????public?void?onGetWalkingRouteResult(MKWalkingRouteResult?result,?int?iError)?{??
  51. ????}??
  52. }??

说明:上面的类定义只是在说明MKSearchListener类的5个方法的作用,全都是空实现,并未给出具体的实现。根据你要检索的内容,再去具体实现上面对应的方法,就能获取到搜索结果。例如:1)你想通过一个地理坐标(经纬度值)来搜索地址信息,那么只需要具体实现上面的onGetAddrResult()方法就能得到搜索结果;2)如果你想搜索驾车路线信息,只需要具体实现onGetDrivingRouteResult()方法就能得到搜索结果。

?

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

[java]?view plaincopy
  1. //?初始化MKSearch??
  2. mMKSearch?=?new?MKSearch();??
  3. mMKSearch.init(mapManager,?new?MySearchListener());??

经过上面两步之后,就可以通过调用MKSearch所提供的一些检索方法来搜索你想要的信息了。
????? 下面给出一个具体的示例:根据某个经纬度值(地理坐标)查询对应的地址信息以及该地址周边的POI(Point of Interest,兴趣点)信息。
1)布局文件res/layout/query_address.xml
?????
[xhtml]?view plaincopy
  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>??

2)继承com.baidu.mapapi.MapActivity的Activity类
??????
[java]?view plaincopy
  1. package?com.liufeng.baidumap;??
  2. ??
  3. import?android.os.Bundle;??
  4. import?android.view.View;??
  5. import?android.view.View.OnClickListener;??
  6. import?android.widget.Button;??
  7. import?android.widget.EditText;??
  8. import?android.widget.TextView;??
  9. ??
  10. import?com.baidu.mapapi.BMapManager;??
  11. import?com.baidu.mapapi.GeoPoint;??
  12. import?com.baidu.mapapi.MKAddrInfo;??
  13. import?com.baidu.mapapi.MKDrivingRouteResult;??
  14. import?com.baidu.mapapi.MKPoiInfo;??
  15. import?com.baidu.mapapi.MKPoiResult;??
  16. import?com.baidu.mapapi.MKSearch;??
  17. import?com.baidu.mapapi.MKSearchListener;??
  18. import?com.baidu.mapapi.MKTransitRouteResult;??
  19. import?com.baidu.mapapi.MKWalkingRouteResult;??
  20. import?com.baidu.mapapi.MapActivity;??
  21. ??
  22. /**?
  23. ?*?根据经纬度查询地址信息?
  24. ?*??
  25. [email protected]?
  26. [email protected]?
  27. ?*/??
  28. public?class?QueryAddressActivity?extends?MapActivity?{??
  29. ????//?定义地图引擎管理类??
  30. ????private?BMapManager?mapManager;??
  31. ????//?定义搜索服务类??
  32. ????private?MKSearch?mMKSearch;??
  33. ??
  34. ????private?EditText?longitudeEditText;??
  35. ????private?EditText?latitudeEditText;??
  36. ????private?TextView?addressTextView;??
  37. ????private?Button?queryButton;??
  38. ??
  39. ????@Override??
  40. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  41. ????????super.onCreate(savedInstanceState);??
  42. ????????setContentView(R.layout.query_address);??
  43. ??
  44. ????????//?初始化MapActivity??
  45. ????????mapManager?=?new?BMapManager(getApplication());??
  46. ????????//?init方法的第一个参数需填入申请的API?Key??
  47. ????????mapManager.init("285B415EBAB2A92293E85502150ADA7F03C777C4",?null);??
  48. ????????super.initMapActivity(mapManager);??
  49. ??
  50. ????????//?初始化MKSearch??
  51. ????????mMKSearch?=?new?MKSearch();??
  52. ????????mMKSearch.init(mapManager,?new?MySearchListener());??
  53. ??
  54. ????????//?通过id查询在布局文件中定义的控件??
  55. ????????longitudeEditText?=?(EditText)?findViewById(R.id.longitude_input);??
  56. ????????latitudeEditText?=?(EditText)?findViewById(R.id.latitude_input);??
  57. ????????addressTextView?=?(TextView)?findViewById(R.id.address_text);??
  58. ????????queryButton?=?(Button)?findViewById(R.id.query_button);??
  59. ??
  60. ????????//?给地址查询按钮设置单击事件监听器??
  61. ????????queryButton.setOnClickListener(new?OnClickListener()?{??
  62. ????????????@Override??
  63. ????????????public?void?onClick(View?v)?{??
  64. ????????????????//?用户输入的经度值??
  65. ????????????????String?longitudeStr?=?longitudeEditText.getText().toString();??
  66. ????????????????//?用户输入的纬度值??
  67. ????????????????String?latitudeStr?=?latitudeEditText.getText().toString();??
  68. ??
  69. ????????????????try?{??
  70. ????????????????????//?将用户输入的经纬度值转换成int类型??
  71. ????????????????????int?longitude?=?(int)?(1000000?*?Double.parseDouble(longitudeStr));??
  72. ????????????????????int?latitude?=?(int)?(1000000?*?Double.parseDouble(latitudeStr));??
  73. ??
  74. ????????????????????//?查询该经纬度值所对应的地址位置信息??
  75. ????????????????????mMKSearch.reverseGeocode(new?GeoPoint(latitude,?longitude));??
  76. ????????????????}?catch?(Exception?e)?{??
  77. ????????????????????addressTextView.setText("查询出错,请检查您输入的经纬度值!");??
  78. ????????????????}??
  79. ????????????}??
  80. ????????});??
  81. ????}??
  82. ??
  83. ????@Override??
  84. ????protected?boolean?isRouteDisplayed()?{??
  85. ????????return?false;??
  86. ????}??
  87. ??
  88. ????@Override??
  89. ????protected?void?onDestroy()?{??
  90. ????????if?(mapManager?!=?null)?{??
  91. ????????????//?程序退出前需调用此方法??
  92. ????????????mapManager.destroy();??
  93. ????????????mapManager?=?null;??
  94. ????????}??
  95. ????????super.onDestroy();??
  96. ????}??
  97. ??
  98. ????@Override??
  99. ????protected?void?onPause()?{??
  100. ????????if?(mapManager?!=?null)?{??
  101. ????????????//?终止百度地图API??
  102. ????????????mapManager.stop();??
  103. ????????}??
  104. ????????super.onPause();??
  105. ????}??
  106. ??
  107. ????@Override??
  108. ????protected?void?onResume()?{??
  109. ????????if?(mapManager?!=?null)?{??
  110. ????????????//?开启百度地图API??
  111. ????????????mapManager.start();??
  112. ????????}??
  113. ????????super.onResume();??
  114. ????}??
  115. ??
  116. ????/**?
  117. ?????*?内部类实现MKSearchListener接口,用于实现异步搜索服务?
  118. ?????*??
  119. [email protected]?
  120. ?????*/??
  121. ????public?class?MySearchListener?implements?MKSearchListener?{??
  122. ????????/**?
  123. ?????????*?根据经纬度搜索地址信息结果?
  124. ?????????*??
  125. [email protected]?
  126. [email protected](0表示正确返回)?
  127. ?????????*/??
  128. ????????@Override??
  129. ????????public?void?onGetAddrResult(MKAddrInfo?result,?int?iError)?{??
  130. ????????????if?(result?==?null)?{??
  131. ????????????????return;??
  132. ????????????}??
  133. ????????????StringBuffer?sb?=?new?StringBuffer();??
  134. ????????????//?经纬度所对应的位置??
  135. ????????????sb.append(result.strAddr).append("/n");??
  136. ??
  137. ????????????//?判断该地址附近是否有POI(Point?of?Interest,即兴趣点)??
  138. ????????????if?(null?!=?result.poiList)?{??
  139. ????????????????//?遍历所有的兴趣点信息??
  140. ????????????????for?(MKPoiInfo?poiInfo?:?result.poiList)?{??
  141. ????????????????????sb.append("----------------------------------------").append("/n");??
  142. ????????????????????sb.append("名称:").append(poiInfo.name).append("/n");??
  143. ????????????????????sb.append("地址:").append(poiInfo.address).append("/n");??
  144. ????????????????????sb.append("经度:").append(poiInfo.pt.getLongitudeE6()?/?1000000.0f).append("/n");??
  145. ????????????????????sb.append("纬度:").append(poiInfo.pt.getLatitudeE6()?/?1000000.0f).append("/n");??
  146. ????????????????????sb.append("电话:").append(poiInfo.phoneNum).append("/n");??
  147. ????????????????????sb.append("邮编:").append(poiInfo.postCode).append("/n");??
  148. ????????????????????//?poi类型,0:普通点,1:公交站,2:公交线路,3:地铁站,4:地铁线路??
  149. ????????????????????sb.append("类型:").append(poiInfo.ePoiType).append("/n");??
  150. ????????????????}??
  151. ????????????}??
  152. ????????????//?将地址信息、兴趣点信息显示在TextView上??
  153. ????????????addressTextView.setText(sb.toString());??
  154. ????????}??
  155. ??
  156. ????????/**?
  157. ?????????*?驾车路线搜索结果?
  158. ?????????*??
  159. [email protected]?
  160. [email protected](0表示正确返回)?
  161. ?????????*/??
  162. ????????@Override??
  163. ????????public?void?onGetDrivingRouteResult(MKDrivingRouteResult?result,?int?iError)?{??
  164. ????????}??
  165. ??
  166. ????????/**?
  167. ?????????*?POI搜索结果(范围检索、城市POI检索、周边检索)?
  168. ?????????*??
  169. [email protected]?
  170. [email protected](11,12,21:poi列表?7:城市列表)?
  171. [email protected](0表示正确返回)?
  172. ?????????*/??
  173. ????????@Override??
  174. ????????public?void?onGetPoiResult(MKPoiResult?result,?int?type,?int?iError)?{??
  175. ????????}??
  176. ??
  177. ????????/**?
  178. ?????????*?公交换乘路线搜索结果?
  179. ?????????*??
  180. [email protected]?
  181. [email protected](0表示正确返回)?
  182. ?????????*/??
  183. ????????@Override??
  184. ????????public?void?onGetTransitRouteResult(MKTransitRouteResult?result,?int?iError)?{??
  185. ????????}??
  186. ??
  187. ????????/**?
  188. ?????????*?步行路线搜索结果?
  189. ?????????*??
  190. [email protected]?
  191. [email protected](0表示正确返回)?
  192. ?????????*/??
  193. ????????@Override??
  194. ????????public?void?onGetWalkingRouteResult(MKWalkingRouteResult?result,?int?iError)?{??
  195. ????????}??
  196. ????}??
  197. }??
???
3)AndroidManifest.xml中的配置
?????
[xhtml]?view plaincopy
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <manifest?xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ??????package="com.liufeng.baidumap"??
  4. ??????android:versionCode="1"??
  5. ??????android:versionName="1.0">??
  6. ????<application?android:icon="@drawable/icon"?android:label="@string/app_name">??
  7. ????????<activity?android:name=".QueryAddressActivity"?android:label="@string/app_name">??
  8. ????????????<intent-filter>??
  9. ????????????????<action?android:name="android.intent.action.MAIN"?/>??
  10. ????????????????<category?android:name="android.intent.category.LAUNCHER"?/>??
  11. ????????????</intent-filter>??
  12. ????????</activity>??
  13. ????</application>??
  14. ????<uses-sdk?android:minSdkVersion="4"?/>??
  15. ??
  16. ????<!--?访问网络的权限?-->??
  17. ????<uses-permission?android:name="android.permission.INTERNET"?/>??
  18. ????<!--?访问精确位置的权限?-->??
  19. ????<uses-permission?android:name="android.permission.ACCESS_FINE_LOCATION"?/>??
  20. ????<!--?访问网络状态的权限?-->??
  21. ????<uses-permission?android:name="android.permission.ACCESS_NETWORK_STATE"?/>??
  22. ????<!--?访问WIFI网络状态的权限?-->??
  23. ????<uses-permission?android:name="android.permission.ACCESS_WIFI_STATE"?/>??
  24. ????<!--?改变WIFI网络状态的权限?-->??
  25. ????<uses-permission?android:name="android.permission.CHANGE_WIFI_STATE"?/>??
  26. ????<!--?读写存储卡的权限?-->??
  27. ????<uses-permission?android:name="android.permission.WRITE_EXTERNAL_STORAGE"?/>??
  28. ????<!--?读取电话状态的权限?-->??
  29. ????<uses-permission?android:name="android.permission.READ_PHONE_STATE"?/>??
  30. </manifest>???

4)运行结果截图及说明
??????
程序在模拟器上运行的初始效果如上图所示。可以看出,地图并没有显示出来,这和我们在设计时布局时所设想的一样;另外两个输入框中也分别显示了默认给出的经纬度值。
????? 点击“地址查询”按钮后,将看到如下图所示包含了查询结果的界面:
??????
说明:图上的“贵州省贵阳市云岩区普陀路”正是我们要查询的地理坐标(经度:106.720397,纬度:26.597239)所对应的地址信息;同时该地址信息下方还显示出了该地址附近的10个兴趣点(POI),每个兴趣点分别包含了“名称”、“地址”、“经纬度”、“电话”、“邮编”和“兴趣点类型”信息。

?

备注:如果本文的示例继续做下去,就应该将MapView显示出来,同时结合第8篇文章“[008] 百度地图API之ItemizedOverlay的使用(Android)”所介绍的内容将地址信息和兴趣点标注在地图上。我想这两方面的内容都已做过详细讲解并给出了示例,再来实现这个应该并不是什么难事,看文章的你就动动手来完成它吧!

  相关解决方案