栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android 高德地图之poi搜索功能的实现代码

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android 高德地图之poi搜索功能的实现代码

废话不多说,先看效果,如果大家感觉不错,请参考实现代码

这个功能我是用Fragmentdialog里面做的,也遇到不少坑

第一,就是设置背景的drawable为纯白色导致键盘弹出的时候,recyclerview的布局被顶上去导致出现白色布局,有点扎眼;最后改成了设置为和背景色一个颜色就和好了

  Window window = getDialog().getWindow();
    WindowManager.LayoutParams lp = window.getAttributes();
    lp.gravity = Gravity.CENTER;
    window.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(getActivity(), R.color.color_gray_f2)));
    window.setAttributes(lp);

布局



  
    
    
    
  
  

第二个问题是键盘弹出的时候,会出现dialog布局整体被顶上去

最后通过设置 style来解决

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //解决dialogfragment布局不被顶上去的方法
    setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar);
  }

最后就是实现搜索功能了

第一个点击搜索时,键盘和搜索按钮两个都是同样的效果


  private void searchLocationPoi() {
    //关闭键盘
    KeyBoardUtils.closeKeybord(poiSearchInMaps, baseApplication.mContext);
    if (TextUtils.isEmpty(poiSearchInMaps.getText().toString().trim())) {
      ToastUtils.showToastCenter("内容为空!");
    } else {
      query = new PoiSearch.Query(poiSearchInMaps.getText().toString().trim(), "", "");// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
      query.setPageSize(20);// 设置每页最多返回多少条poiitem
      query.setPageNum(0);// 设置查第一页
      poiSearch = new PoiSearch(getActivity(), query);
      poiSearch.setonPoiSearchListener(this);
      poiSearch.searchPOIAsyn();
    }
  }

然后回调中进行处理

@Override
  public void onPoiSearched(PoiResult poiResult, int errcode) {
    Logger.e(poiResult.getPois().toString() + "" + errcode);
    if (errcode == 1000) {
      datas = new ArrayList<>();
      ArrayList pois = poiResult.getPois();
      for (int i = 0; i < pois.size(); i++) {
 LocationBean locationBean = new LocationBean();
 locationBean.title = pois.get(i).getTitle();
 locationBean.snippet = pois.get(i).getSnippet();
 datas.add(locationBean);
      }
      searchCarAdapter.setNewData(datas);
    }
  }

    还有就是监听EditText里面内容的变化来搜索,其实也很简单

 poiSearchInMaps.addTextChangedListener(new TextWatcher() {
      @Override
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
      }
      @Override
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
 textChangeSearch(charSequence);
      }
      @Override
      public void afterTextChanged(Editable editable) {
      }
    });
  
  private void textChangeSearch(CharSequence charSequence) {
    String content = charSequence.toString().trim();//获取自动提示输入框的内容
    Logger.e(content);
    InputtipsQuery inputtipsQuery = new InputtipsQuery(content, "");//初始化一个输入提示搜索对象,并传入参数
    Inputtips inputtips = new Inputtips(getActivity(), inputtipsQuery);//定义一个输入提示对象,传入当前上下文和搜索对象
    inputtips.setInputtipsListener(new Inputtips.InputtipsListener() {
      @Override
      public void onGetInputtips(List list, int errcode) {
 Logger.e(list.toString() + errcode);
 if (errcode == 1000 && list != null) {
   datas = new ArrayList<>();
   for (int i = 0; i < list.size(); i++) {
     LocationBean locationBean = new LocationBean();
     Tip tip = list.get(i);
     locationBean.latitude = tip.getPoint().getLatitude();
     locationBean.longitude = tip.getPoint().getLongitude();
     locationBean.snippet = tip.getName();
     locationBean.title = tip.getDistrict();
     datas.add(locationBean);
   }
   searchCarAdapter.setNewData(datas);
 }
      }
    });//设置输入提示查询的监听,实现输入提示的监听方法onGetInputtips()
    inputtips.requestInputtipsAsyn();//输入查询提示的异步接口实现
  }

ok,搞定,最后只需要搞个回调,把Search后点击的item传回去就好了.希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/157976.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号