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

來源:互聯網
上載者:User

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

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. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.