Android中使用百度地圖API:城市POI搜尋-擷取所有結果

來源:互聯網
上載者:User

本文主要講解如何通過百度地圖API搜尋得到一個城市裡的所有POI。這裡有必要對“所有”這個詞進行強 調一下,以便引起重視,之所以這樣說,是因為在搜尋POI時,預設僅返回一頁的搜尋結果10條,那麼如何才 能得到所有的搜尋結果呢?其實baidu map api是提供了相關的方法,但我發現有相當多的網友都在問這個問 題,所以有必要講解示範一下。

先講一下什麼稱之為“城市POI搜尋”?它與我們在上一篇文章([011] 百 度地圖API之POI搜尋-發現你身邊的興趣點,如超市、餐廳、ATM...(Android))中瞭解到的POI搜尋有什麼區 別呢?

上一篇文章中所調用的是地圖API的“周邊POI搜尋”服務,即檢索周圍多少米以內的POI;而本章所 要調用的是地圖API的“城市POI搜尋”服務,即檢索某個城市中所有的POI。如果你看完這兩篇文章後,你會 發現僅僅是調用的方法不同而以,搜尋結果的處理方法是同一個方法,搜尋結果的處理代碼也是完全一樣的。

下面將給出城市POI搜尋的一個完整樣本,並且會講解如何才能擷取到所有的搜尋結果。

1)布局檔案 res/layout/poi_city_search.xml

<?xml version="1.0" encoding="utf-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >      <com.baidu.mapapi.MapView android:id="@+id/map_View"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:clickable="true"    />      <LinearLayout        android:orientation="horizontal"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignTop="@id/map_View"        android:layout_alignLeft="@id/map_View"        android:layout_alignRight="@id/map_View"        android:background="@null"        android:padding="0dip"        >          <EditText android:id="@+id/keyword_edittext"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical"            android:layout_weight="20" />          <Button android:id="@+id/query_button"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_vertical"            android:layout_weight="1"            android:text="搜尋" />      </LinearLayout>  </RelativeLayout>

2)繼承了com.baidu.mapapi.MapActivity的Activity類

package com.liufeng.baidumap;        import android.app.AlertDialog;  import android.content.DialogInterface;  import android.os.Bundle;  import android.view.View;  import android.view.View.OnClickListener;  import android.widget.Button;  import android.widget.EditText;        import com.baidu.mapapi.BMapManager;  import com.baidu.mapapi.MKAddrInfo;  import com.baidu.mapapi.MKDrivingRouteResult;  import com.baidu.mapapi.MKPoiInfo;  import com.baidu.mapapi.MKPoiResult;  import com.baidu.mapapi.MKSearch;  import com.baidu.mapapi.MKSearchListener;  import com.baidu.mapapi.MKTransitRouteResult;  import com.baidu.mapapi.MKWalkingRouteResult;  import com.baidu.mapapi.MapActivity;  import com.baidu.mapapi.MapController;  import com.baidu.mapapi.MapView;  import com.baidu.mapapi.PoiOverlay;        public class PoiSearchInCityActivity extends MapActivity {      // 定義地圖引擎管理類      private BMapManager mapManager;      // 定義搜尋服務類      private MKSearch mMKSearch;            private MapView mapView;      private MapController mapController;      private EditText keyWordEditText;      private Button queryButton;      private static StringBuilder sb;                @Override    public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.poi_city_search);                // 初始化MapActivity          mapManager = new BMapManager(getApplication());          // init方法的第一個參數需填入申請的API Key          mapManager.init("285B415EBAB2A92293E85502150ADA7F03C777C4", null);          super.initMapActivity(mapManager);                mapView = (MapView) findViewById(R.id.map_View);          // 設定地圖模式為交通地圖          mapView.setTraffic(true);          // 設定啟用內建的縮放控制項          mapView.setBuiltInZoomControls(true);                // 取得地圖控制器對象,用於控制MapView          mapController = mapView.getController();          // 設定地圖預設的縮放層級          mapController.setZoom(10);                // 設定每頁返回的POI數,預設為10,取值範圍1-50          MKSearch.setPoiPageCapacity(10);          // 初始化MKSearch          mMKSearch = new MKSearch();          mMKSearch.init(mapManager, new MySearchListener());                        keyWordEditText = (EditText) findViewById(R.id.keyword_edittext);          queryButton = (Button) findViewById(R.id.query_button);          queryButton.setOnClickListener(new OnClickListener() {              @Override            public void onClick(View v) {                  //每次搜尋前先前sb中的內容清空                  sb = new StringBuilder();                        String keyWord = keyWordEditText.getText().toString().trim();                  // 搜尋貴陽地區的沃爾瑪                  mMKSearch.poiSearchInCity("貴陽", keyWord);              }          });      }            @Override    protected boolean isRouteDisplayed() {          return false;      }            @Override    protected void onDestroy() {          if (mapManager != null) {              // 程式退出前需調用此方法              mapManager.destroy();              mapManager = null;          }          super.onDestroy();      }            @Override    protected void onPause() {          if (mapManager != null) {              // 終止百度地圖API              mapManager.stop();          }          super.onPause();      }            @Override    protected void onResume() {          if (mapManager != null) {              // 開啟百度地圖API              mapManager.start();          }          super.onResume();      }            /**      * 實現MKSearchListener介面,用於實現非同步搜尋服務      *       * @author liufeng      */    public class MySearchListener implements MKSearchListener {          /**          * 根據經緯度搜尋地址資訊結果          *           * @param result 搜尋結果          * @param iError 錯誤號碼(0表示正確返回)          */        @Override        public void onGetAddrResult(MKAddrInfo result, int iError) {          }                /**          * 駕車路線搜尋結果          *           * @param result 搜尋結果          * @param iError 錯誤號碼(0表示正確返回)          */        @Override        public void onGetDrivingRouteResult(MKDrivingRouteResult result, int iError) {          }                /**          * POI搜尋結果(範圍檢索、城市POI檢索、周邊檢索)          *           * @param result 搜尋結果          * @param type 返回結果類型(11,12,21:poi列表 7:城市列表)          * @param iError 錯誤號碼(0表示正確返回)          */        @Override        public void onGetPoiResult(MKPoiResult result, int type, int iError) {              if (result == null) {                  return;              }              // 清除地圖上已有的所有覆蓋物              mapView.getOverlays().clear();              // PoiOverlay是baidu map api提供的用於顯示POI的Overlay              PoiOverlay poioverlay = new PoiOverlay(PoiSearchInCityActivity.this, mapView);              // 設定搜尋到的POI資料              poioverlay.setData(result.getAllPoi());              // 在地圖上顯示PoiOverlay(將搜尋到的興趣點標註在地圖上)              mapView.getOverlays().add(poioverlay);                    if(result.getNumPois() > 0) {                  // 設定其中一個搜尋結果所在地理座標為地圖的中心                  MKPoiInfo poiInfo = result.getPoi(0);                  mapController.setCenter(poiInfo.pt);              }                                sb.append("共搜尋到").append(result.getNumPois()).append("個POI/n");              // 遍曆當前頁返回的POI(預設只返回10個)              for (MKPoiInfo poiInfo : result.getAllPoi()) {                  sb.append("名稱:").append(poiInfo.name).append("/n");                  //sb.append("地址:").append(poiInfo.address).append("/n");                  //sb.append("經度:").append(poiInfo.pt.getLongitudeE6() / 1000000.0f).append("/n");                  //sb.append("緯度:").append(poiInfo.pt.getLatitudeE6() / 1000000.0f).append("/n");              }                    // 通過AlertDialog顯示當前頁搜尋到的POI              new AlertDialog.Builder(PoiSearchInCityActivity.this)              .setTitle("搜尋到的POI資訊")              .setMessage(sb.toString())              .setPositiveButton("關閉", new DialogInterface.OnClickListener() {                  public void onClick(DialogInterface dialog, int whichButton) {                      dialog.dismiss();                  }              }).create().show();          }                /**          * 公交轉乘路線搜尋結果          *           * @param result 搜尋結果          * @param iError 錯誤號碼(0表示正確返回)          */        @Override        public void onGetTransitRouteResult(MKTransitRouteResult result, int iError) {          }                /**          * 步行路線搜尋結果          *           * @param result 搜尋結果          * @param iError 錯誤號碼(0表示正確返回)          */        @Override        public void onGetWalkingRouteResult(MKWalkingRouteResult result, int iError) {          }      }  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.