Android百度地圖API整合三《搜尋》,android《搜尋》

來源:互聯網
上載者:User

Android百度地圖API整合三《搜尋》,android《搜尋》

  書接上回↑

  一、基礎地圖介面地址:http://www.cnblogs.com/dhr125/p/5969980.html

  二、地圖定位地址:http://www.cnblogs.com/dhr125/p/5970118.html

 

搜尋功能

1.在xml檔案中加入布局

 1 <LinearLayout 2         android:layout_width="match_parent" 3         android:layout_height="wrap_content"> 4         <EditText 5             android:layout_width="0dip" 6             android:layout_height="wrap_content" 7             android:layout_weight="1" 8             android:hint="請輸入搜尋地址" 9             android:id="@+id/etsearch"/>10         <Button11             android:layout_width="wrap_content"12             android:layout_height="wrap_content"13             android:text="搜尋"14             android:id="@+id/btnsearch"/>15     </LinearLayout>

 2.按照位置,找到PoiSearchDemo.java,準備複製代碼

3.複製下面一段代碼到onCreate中,然後讓Activity實現OnGetPoiSearchResultListener 

 // 初始化搜尋模組,註冊搜尋事件監聽    mPoiSearch = PoiSearch.newInstance();    mPoiSearch.setOnGetPoiSearchResultListener(this);

4.實現後會重寫下面3個方法

 1  @Override 2     public void onGetPoiResult(PoiResult poiResult) { 3  4     } 5  6     @Override 7     public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) { 8  9     }10 11     @Override12     public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {13 14     }

 

5.向重寫的方法onGetPoiResult中加入代碼,然後根據報錯修改代碼

 1  if (poiResult == null || poiResult.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) { 2             Toast.makeText(MainActivity.this, "未找到結果", Toast.LENGTH_LONG) 3                     .show(); 4             return; 5         } 6         if (poiResult.error == SearchResult.ERRORNO.NO_ERROR) { 7             mBaiduMap.clear(); 8             PoiOverlay overlay = new MyPoiOverlay(mBaiduMap); 9             mBaiduMap.setOnMarkerClickListener(overlay);10             overlay.setData(poiResult);11             overlay.addToMap();12             overlay.zoomToSpan();13                     showNearbyArea(center, radius);14 15             return;16         }

6.按照下面位置找到兩個類,複製到項目中

7.向工程中加入代碼

 1 private class MyPoiOverlay extends PoiOverlay { 2  3         public MyPoiOverlay(BaiduMap baiduMap) { 4             super(baiduMap); 5         } 6  7         @Override 8         public boolean onPoiClick(int index) { 9             super.onPoiClick(index);10             PoiInfo poi = getPoiResult().getAllPoi().get(index);11             // if (poi.hasCaterDetails) {12             mPoiSearch.searchPoiDetail((new PoiDetailSearchOption())13                     .poiUid(poi.uid));14             // }15             return true;16         }17     }
 1  /** 2      * 對周邊檢索的範圍進行繪製 3      * @param center 4      * @param radius 5      */ 6     public void showNearbyArea( LatLng center, int radius) { 7      16     }

 

 8.將下面兩個成員變數加入成員位置

  LatLng center = null;  int radius = 5000;

  並且,在 定位SDK監聽函數MyLocationListenner中if判斷後,執行個體化center,如下

 @Override        public void onReceiveLocation(BDLocation location) {            // map view 銷毀後不在處理新接收的位置            if (location == null || mMapView == null) {                return;            }            center = new LatLng(location.getLatitude(),location.getLongitude());

 9.找到自訂的輸入框和按鈕,監聽

 

 1      etsearch = (EditText) findViewById(R.id.etsearch); 2         Button btnsearch = (Button) findViewById(R.id.btnsearch); 3         btnsearch.setOnClickListener(new View.OnClickListener() { 4             @Override 5             public void onClick(View v) { 6                 PoiNearbySearchOption nearbySearchOption = new PoiNearbySearchOption().keyword(etsearch.getText() 7                         .toString()).sortType(PoiSortType.distance_from_near_to_far).location(center) 8                         .radius(radius).pageNum(10); 9                 mPoiSearch.searchNearby(nearbySearchOption);10             }11         });

 

 以上,即可實現搜尋功能

  

 

 

 

 

聯繫我們

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