編輯:關於 Android 開發
書接上回:
一、基礎地圖界面: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 });
以上步驟完成後,即可實現周邊搜索功能。

Android UI 組件深入:ImageButton 與 ZoomButton 圖文解析
透視 ImageButton 與 ZoomButton:讓圖片按鈕真正「活」起來 單看 ImageButton 這個名字,很容易以為它就是一個普通的圖片按鈕,把圖片往上一
Android 加固系列(五):從 JNI 層 Hook 系統 API 談起
Android 加固系列(五):從 JNI 層 Hook 系統 API 談起 【版權所有,轉載請註明出處】 加固與破解是一體兩面:唯有先理解攻擊者會從哪些環節下手,才知道
Android 計算器 Pro 項目源碼解析
Android 計算器 Pro 項目源碼解析 這個計算器 Pro 專案主要示範 Android 應用中常見的數字輸入、運算符處理與結果顯示流程。雖然功能相對簡單,但很適合
Android Handler深入解析:面試必問的訊息處理機制
Android Handler深入解析:面試必問的訊息處理機制 Handler在Android中被稱為「訊息處理者」,在多執行緒開發中應用極為廣泛,幾乎是每場面試都會觸及