Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 百度地圖 API 集成(三):實作周邊搜索功能

編輯:關於 Android 開發

Android 百度地圖 API 集成(三):實作周邊搜索功能

書接上回:

一、基礎地圖界面:https://www.cnblogs.com/dhr125/p/5969980.html

二、地圖定位:https://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 <Button 11 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. 實作後需重寫以下三個方法:

 1 @Override 2 public void onGetPoiResult(PoiResult poiResult) { 3 4 } 5 6 @Override 7 public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) { 8 9 } 10 11 @Override 12 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 });

以上步驟完成後,即可實現周邊搜索功能。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved