ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Android学习笔记之百度地图(根据地名查询经纬度)

Android学习笔记之百度地图(根据地名查询经纬度)

原创 Linux操作系统 作者:itbole1 时间:2013-05-19 19:24:04 0 删除 编辑

重要方法:

public int geocode(java.lang.String strAddr, java.lang.String city)

根据地址名获取地址信息

www.itbole.net发表

异步函数,返回结果在MKSearchListener里的onGetAddrResult方法通知 参数: strAddr - 地址名 city - 地址所在城市 返回:

成功返回0,否则返回-1


具体实现:

  1. package xiaosi.baiduMap;
  2. import android.app.AlertDialog;
  3. import android.content.DialogInterface;
  4. import android.os.Bundle;
  5. import com.baidu.mapapi.BMapManager;
  6. import com.baidu.mapapi.MKAddrInfo;
  7. import com.baidu.mapapi.MKDrivingRouteResult;
  8. import com.baidu.mapapi.MKPoiResult;
  9. import com.baidu.mapapi.MKSearch;
  10. import com.baidu.mapapi.MKSearchListener;
  11. import com.baidu.mapapi.MKTransitRouteResult;
  12. import com.baidu.mapapi.MKWalkingRouteResult;
  13. import com.baidu.mapapi.MapActivity;
  14. import com.baidu.mapapi.MapController;
  15. import com.baidu.mapapi.MapView;
  16. public class BaiduMapActivity extends MapActivity
  17. {
  18. /** Called when the activity is first created. */
  19. private BMapManager mapManager = null;
  20. private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";
  21. private MapView mapView = null;
  22. private MKSearch mKSearch;
  23. private MapController mapController = null;
  24. @Override
  25. public void onCreate(Bundle savedInstanceState)
  26. {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.main);
  29. init();
  30. }
  31. private void init()
  32. {
  33. mapManager = new BMapManager(getApplication());
  34. mapManager.init(key, null);
  35. super.initMapActivity(mapManager);
  36. mapView = (MapView) findViewById(R.id.mapView);
  37. // 设置启用内置的缩放控件
  38. mapView.setBuiltInZoomControls(true);
  39. // 得到mMapView的控制权,可以用它控制和驱动平移和缩放
  40. mapController = mapView.getController();
  41. // 设置地图zoom级别
  42. mapController.setZoom(12);
  43. mKSearch = new MKSearch();
  44. // 注意,MKSearchListener只支持一个,以最后一次设置为准
  45. mKSearch.init(mapManager, new MySearchListener());
  46. if (mKSearch.geocode("五四广场", "青岛") == 0)
  47. {
  48. System.out.println("搜索成功");
  49. }
  50. else
  51. {
  52. System.out.println("搜索失败");
  53. }
  54. }
  55. public class MySearchListener implements MKSearchListener
  56. {
  57. public void onGetAddrResult(MKAddrInfo arg0, int arg1)
  58. {
  59. /*
  60. * 返回地址信息搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示结果正确,result中有相关结果信息;100表示结果正确,无相关地址信息
  61. */
  62. String Location = null;
  63. if (arg0 == null)
  64. {
  65. Location = "没有搜索到该地址";
  66. }
  67. else
  68. {
  69. // 获得搜索地址的经纬度
  70. Location = "纬度:" + arg0.geoPt.getLatitudeE6() / 1E6 + "\n"
  71. + "经度:" + arg0.geoPt.getLongitudeE6() / 1E6 + "\n";
  72. mapController.animateTo(arg0.geoPt);
  73. }
  74. AlertDialog.Builder builder = new AlertDialog.Builder(
  75. BaiduMapActivity.this);
  76. builder.setTitle("搜索结果");
  77. builder.setMessage(Location);
  78. builder.setPositiveButton("关闭",
  79. new android.content.DialogInterface.OnClickListener() {
  80. public void onClick(DialogInterface dialog, int which)
  81. {
  82. dialog.dismiss();
  83. }
  84. });
  85. builder.show();
  86. }
  87. public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)
  88. {
  89. /*
  90. * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回
  91. */
  92. }
  93. public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)
  94. {
  95. /*
  96. * 返回poi搜索结果。 参数: arg0 - 搜索结果 arg1 - 返回结果类型: MKSearch.TYPE_POI_LIST MKSearch.TYPE_AREA_POI_LIST MKSearch.TYPE_CITY_LIST arg2 - 错误号,0表示正确返回
  97. */
  98. }
  99. public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)
  100. {
  101. /*
  102. * 返回公交搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回, 当返回MKEvent.ERROR_ROUTE_ADDR时,表示起点或终点有歧义, 调用MKTransitRouteResult的getAddrResult方法获取推荐的起点或终点信息
  103. */
  104. }
  105. public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)
  106. {
  107. /*
  108. * 返回步行路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回
  109. */
  110. }
  111. }
  112. @Override
  113. protected boolean isRouteDisplayed()
  114. {
  115. return false;
  116. }
  117. @Override
  118. protected void onDestroy()
  119. {
  120. if (mapManager != null)
  121. {
  122. mapManager.destroy();
  123. mapManager = null;
  124. }
  125. super.onDestroy();
  126. }
  127. @Override
  128. protected void onPause()
  129. {
  130. if (mapManager != null)
  131. {
  132. mapManager.stop();
  133. }
  134. super.onPause();
  135. }
  136. @Override
  137. protected void onResume()
  138. {
  139. if (mapManager != null)
  140. {
  141. mapManager.start();
  142. }
  143. super.onResume();
  144. }
  145. }

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28901198/viewspace-761470/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-05-19

  • 博文量
    17
  • 访问量
    32836