书接上回↑

一、基础地图界面地址:http://www.cnblogs.com/dhr125/p/5969980.html

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

搜索功能

1.在xml文件中加入布局

<LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content">
         <EditText
             android:layout_width="0dip"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:hint="请输入搜索地址"
             android:id="@+id/etsearch"/>
         <Button
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="搜索"
             android:id="@+id/btnsearch"/>
     </LinearLayout>

 2.按照下图位置,找到PoiSearchDemo.java,准备复制代码

3.复制下面一段代码到onCreate中,然后让Activity实现OnGetPoiSearchResultListener 

// 初始化搜索模块,注册搜索事件监听
    mPoiSearch = PoiSearch.newInstance();
    mPoiSearch.setOnGetPoiSearchResultListener(this);

4.实现后会重写下面3个方法

@Override
     public void onGetPoiResult(PoiResult poiResult) {
 
     }
 
     @Override
     public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
 
     }
 
     @Override
     public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
 
     }

5.向重写的方法onGetPoiResult中加入代码,然后根据报错修改代码

if (poiResult == null || poiResult.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
             Toast.makeText(MainActivity.this, "未找到结果", Toast.LENGTH_LONG)
                     .show();
             return;
         }
         if (poiResult.error == SearchResult.ERRORNO.NO_ERROR) {
             mBaiduMap.clear();
             PoiOverlay overlay = new MyPoiOverlay(mBaiduMap);
             mBaiduMap.setOnMarkerClickListener(overlay);
             overlay.setData(poiResult);
             overlay.addToMap();
             overlay.zoomToSpan();
                     showNearbyArea(center, radius);
 
             return;
         }

6.按照下面位置找到两个类,复制到项目中

Android百度地图API集成三《搜索》

7.向工程中加入代码

private class MyPoiOverlay extends PoiOverlay {
 
         public MyPoiOverlay(BaiduMap baiduMap) {
             super(baiduMap);
         }
 
         @Override
         public boolean onPoiClick(int index) {
             super.onPoiClick(index);
             PoiInfo poi = getPoiResult().getAllPoi().get(index);
             // if (poi.hasCaterDetails) {
             mPoiSearch.searchPoiDetail((new PoiDetailSearchOption())
                     .poiUid(poi.uid));
             // }
             return true;
         }
     }
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.找到自定义的输入框和按钮,监听

    etsearch = (EditText) findViewById(R.id.etsearch);
         Button btnsearch = (Button) findViewById(R.id.btnsearch);
         btnsearch.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 PoiNearbySearchOption nearbySearchOption = new PoiNearbySearchOption().keyword(etsearch.getText()
                         .toString()).sortType(PoiSortType.distance_from_near_to_far).location(center)
                         .radius(radius).pageNum(10);
                 mPoiSearch.searchNearby(nearbySearchOption);
             }
         });

 以上,即可实现搜索功能

Android百度地图API集成三《搜索》