百度地图集成——搜索

搜索功能

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 

1
2
3
// 初始化搜索模块,注册搜索事件监听
   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     @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.将下面两个成员变量加入成员位置

1
2
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         });
复制代码
 
以上代码集成后即可搜索周边


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值