POI搜索有三种方式。依据范围和检索词发起范围检索poiSearchInbounds。城市poi检索poiSearchInCity,周边检索poiSearchNearBy。

下以周边检索为例介绍怎样进行检索并显示覆盖物PoiOverlay:

public int poiSearchNearBy(java.lang.String key, 

title=Android%E5%B9%B3%E5%8F%B0/%E7%B1%BB%E5%8F%82%E8%80%83/GeoPoint" title="Android平台/类參考/GeoPoint" style="color:rgb(38,121,186); text-decoration:none">GeoPoint pt,
int radius)

依据中心点、半径与检索词发起周边检索. 


异步函数,返回结果在MKSearchListener里的onGetPoiResult方法通知
參数:

key - 关键词
pt - 中心点地理坐标
radius - 半径。单位:米
返回:

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

Demo: 检索天安门周边5000米之内的KFC餐厅


[java] view
plain
copy

  1. mMKSearch.poiSearchNearBy("KFC", new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)), 5000);




实现MySearchListener的onGetPoiResult,并展示检索结果:
[java] view
plain
copy

  1. public void onGetPoiResult(MKPoiResult result, int type, int iError) {
  2. if (result == null) {
  3. return;
  4. }
  5. PoiOverlay poioverlay = new PoiOverlay(MyMapActivity.this, mMapView);
  6. poioverlay.setData(result.getAllPoi());
  7. mMapView.getOverlays().add(poioverlay);
  8. }



详细实现:
[java] view
plain
copy

  1. package xiaosi.baiduMap;
  2. import android.os.Bundle;
  3. import com.baidu.mapapi.BMapManager;
  4. import com.baidu.mapapi.GeoPoint;
  5. import com.baidu.mapapi.MKAddrInfo;
  6. import com.baidu.mapapi.MKDrivingRouteResult;
  7. import com.baidu.mapapi.MKPoiResult;
  8. import com.baidu.mapapi.MKSearch;
  9. import com.baidu.mapapi.MKSearchListener;
  10. import com.baidu.mapapi.MKTransitRouteResult;
  11. import com.baidu.mapapi.MKWalkingRouteResult;
  12. import com.baidu.mapapi.MapActivity;
  13. import com.baidu.mapapi.MapController;
  14. import com.baidu.mapapi.MapView;
  15. import com.baidu.mapapi.PoiOverlay;
  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. @Override
  23. public void onCreate(Bundle savedInstanceState)
  24. {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.main);
  27. mapManager = new BMapManager(getApplication());
  28. mapManager.init(key, null);
  29. super.initMapActivity(mapManager);
  30. mapView = (MapView) findViewById(R.id.mapsView);
  31. mapView.setBuiltInZoomControls(true); // 设置启用内置的缩放控件
  32. MapController mapController = mapView.getController(); // 得到mMapView的控制权,能够用它控制和驱动平移和缩放
  33. mapController.setZoom(12); // 设置地图zoom级别
  34. MKSearch mKSearch = new MKSearch();
  35. mKSearch.init(mapManager, new MySearchListener());// 注意。MKSearchListener仅仅支持一个。以最后一次设置为准
  36. mKSearch.poiSearchNearBy("KFC", new GeoPoint((int) (39.915 * 1E6),
  37. (int) (116.404 * 1E6)), 5000);
  38. }
  39. public class MySearchListener implements MKSearchListener
  40. {
  41. public void onGetAddrResult(MKAddrInfo arg0, int arg1)
  42. {}
  43. public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)
  44. {}
  45. public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)
  46. {
  47. if (arg0 == null) {
  48. return;
  49. }
  50. PoiOverlay poioverlay = new PoiOverlay(BaiduMapActivity.this, mapView);
  51. poioverlay.setData(arg0.getAllPoi());
  52. mapView.getOverlays().add(poioverlay);
  53. }
  54. public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)
  55. {}
  56. public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)
  57. {}
  58. }
  59. @Override
  60. protected boolean isRouteDisplayed()
  61. {
  62. return false;
  63. }
  64. @Override
  65. protected void onDestroy()
  66. {
  67. if (mapManager != null)
  68. {
  69. mapManager.destroy();
  70. mapManager = null;
  71. }
  72. super.onDestroy();
  73. }
  74. @Override
  75. protected void onPause()
  76. {
  77. if (mapManager != null)
  78. {
  79. mapManager.stop();
  80. }
  81. super.onPause();
  82. }
  83. @Override
  84. protected void onResume()
  85. {
  86. if (mapManager != null)
  87. {
  88. mapManager.start();
  89. }
  90. super.onResume();
  91. }
  92. }

最新文章

  1. CSS动画与GPU
  2. Java学习笔记 06 数字格式化及数学运算
  3. HDU5853 Jong Hyok and String(二分 + 后缀数组)
  4. 如何单独启动wamp 中自带的MySQL
  5. poj3904
  6. 自定义控件(视图)2期笔记08:自定义控件之 9patch图说明
  7. ubuntu下安装多个jdk的切换命令update-alternatives
  8. UEP-下拉
  9. BZOJ_1833_[ZJOI2010]count 数字计数_数位DP
  10. Kubernetes系列02—Kubernetes设计架构和设计理念
  11. 第41章 CORS - Identity Server 4 中文文档(v1.0.0)
  12. swipe.js实现支持手拔与自动切换的图片轮播
  13. azkaban工作流调度器及相关工具对比
  14. mysql 开发基础系列10 存储引擎 InnoDB 介绍
  15. kubernetes学习笔记之十一:kubernetes dashboard认证及分级授权
  16. CSS 选用字体
  17. 寒假生活第一天——Github初体验
  18. oracle 导出表
  19. Java 垃圾回收思维导图
  20. React 回忆录(一)为什么使用 React?

热门文章

  1. 左右分栏页面右侧无法出现滚动条bug
  2. 2011 luogu P1311 选择客栈
  3. Educational Codeforces Round 59 (Rated for Div. 2) (前四题)
  4. 洛谷 P4073 [WC2013]平面图
  5. 我的MySql掉队了
  6. 内存区--Java
  7. 前端面试绝对会考的JS问题!【已经开源】
  8. 条款23:宁一 non-member no-friend 替换member函数(prefer non-member non-friend functions to members functions)
  9. 计蒜客 Overlapping Rectangles (离散化)
  10. SQL 一次插入多条记录