Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android POI搜索,附近搜索,周邊搜索定位介紹

android POI搜索,附近搜索,周邊搜索定位介紹

編輯:關於Android編程

POI搜索有三種方式,根據范圍和檢索詞發起范圍檢索poiSearchInbounds,城市poi檢索poiSearchInCity,周邊檢索poiSearchNearBy。

下以周邊檢索為例介紹如何進行檢索並顯示覆蓋物PoiOverlay:

 

public int poiSearchNearBy(java.lang.String key, GeoPoint pt, int radius)

根據中心點、半徑與檢索詞發起周邊檢索.
 
異步函數,返回結果在MKSearchListener裡的onGetPoiResult方法通知
參數:
key - 關鍵詞
pt - 中心點地理坐標
radius - 半徑,單位:米
返回:
成功返回0,否則返回-1 Demo: 檢索天安門周邊5000米之內的KFC餐廳
[java]
  1. mMKSearch.poiSearchNearBy(KFC, new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)), 5000);
    \


    實現MySearchListener的onGetPoiResult,並展示檢索結果:
    [java] 
    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] 
      1. package xiaosi.baiduMap;
      2.  
      3. import android.os.Bundle;
      4.  
      5. import com.baidu.mapapi.BMapManager;
      6. import com.baidu.mapapi.GeoPoint;
      7. import com.baidu.mapapi.MKAddrInfo;
      8. import com.baidu.mapapi.MKDrivingRouteResult;
      9. import com.baidu.mapapi.MKPoiResult;
      10. import com.baidu.mapapi.MKSearch;
      11. import com.baidu.mapapi.MKSearchListener;
      12. import com.baidu.mapapi.MKTransitRouteResult;
      13. import com.baidu.mapapi.MKWalkingRouteResult;
      14. import com.baidu.mapapi.MapActivity;
      15. import com.baidu.mapapi.MapController;
      16. import com.baidu.mapapi.MapView;
      17. import com.baidu.mapapi.PoiOverlay;
      18.  
      19. public class BaiduMapActivity extends MapActivity
      20. {
      21. /** Called when the activity is first created. */
      22. private BMapManager mapManager = null;
      23. private String key = 1B79478DA01F7800AEA8602517A6D89B38151105;
      24. private MapView mapView = null;
      25.  
      26. @Override
      27. public void onCreate(Bundle savedInstanceState)
      28. {
      29. super.onCreate(savedInstanceState);
      30. setContentView(R.layout.main);
      31. mapManager = new BMapManager(getApplication());
      32. mapManager.init(key, null);
      33. super.initMapActivity(mapManager);
      34. mapView = (MapView) findViewById(R.id.mapsView);
      35. mapView.setBuiltInZoomControls(true); // 設置啟用內置的縮放控件
      36. MapController mapController = mapView.getController(); // 得到mMapView的控制權,可以用它控制和驅動平移和縮放
      37. mapController.setZoom(12); // 設置地圖zoom級別
      38.  
      39.  
      40. MKSearch mKSearch = new MKSearch();
      41. mKSearch.init(mapManager, new MySearchListener());// 注意,MKSearchListener只支持一個,以最後一次設置為准
      42. mKSearch.poiSearchNearBy(KFC, new GeoPoint((int) (39.915 * 1E6),
      43. (int) (116.404 * 1E6)), 5000);
      44. }
      45.  
      46. public class MySearchListener implements MKSearchListener
      47. {
      48. public void onGetAddrResult(MKAddrInfo arg0, int arg1)
      49. {}
      50.  
      51. public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)
      52. {}
      53.  
      54. public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)
      55. {
      56. if (arg0 == null) {
      57. return;
      58. }
      59. PoiOverlay poioverlay = new PoiOverlay(BaiduMapActivity.this, mapView);
      60. poioverlay.setData(arg0.getAllPoi());
      61. mapView.getOverlays().add(poioverlay);
      62. }
      63.  
      64. public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)
      65. {}
      66.  
      67. public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)
      68. {}
      69. }
      70.  
      71. @Override
      72. protected boolean isRouteDisplayed()
      73. {
      74. return false;
      75. }
      76.  
      77. @Override
      78. protected void onDestroy()
      79. {
      80. if (mapManager != null)
      81. {
      82. mapManager.destroy();
      83. mapManager = null;
      84. }
      85. super.onDestroy();
      86. }
      87.  
      88. @Override
      89. protected void onPause()
      90. {
      91. if (mapManager != null)
      92. {
      93. mapManager.stop();
      94. }
      95. super.onPause();
      96. }
      97.  
      98. @Override
      99. protected void onResume()
      100. {
      101. if (mapManager != null)
      102. {
      103. mapManager.start();
      104. }
      105. super.onResume();
      106. }
      107. }
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved