Android 高德地图之poi搜索功能的实现代码
作者:villa_mou 发布时间:2022-03-07 01:36:52
废话不多说,先看效果,如果大家感觉不错,请参考实现代码
这个功能我是用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);
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/color_gray_f2"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/search_maps_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:background="@drawable/new_card">
<ImageButton
android:id="@+id/dialog_search_back"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_margin="2dp"
android:background="@drawable/button_background_selector"
android:src="@drawable/ic_qu_appbar_back"/>
<ImageButton
android:id="@+id/dialog_serach_btn_search"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="2dp"
android:background="@drawable/button_background_selector"
android:src="@drawable/ic_qu_search"
tools:ignore="ContentDescription,RtlHardcoded"/>
<EditText
android:id="@+id/dialog_search_et"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_marginLeft="5.0dip"
android:layout_marginRight="5.0dip"
android:layout_toLeftOf="@+id/dialog_serach_btn_search"
android:layout_toRightOf="@+id/dialog_search_back"
android:background="@android:color/transparent"
android:completionThreshold="1"
android:dropDownVerticalOffset="1.0dip"
android:hint="请输入关键字"
android:imeOptions="actionSearch|flagNoExtractUi"
android:inputType="text|textAutoComplete"
android:maxHeight="50dp"
android:maxLength="20"
android:minHeight="50dp"
android:singleLine="true"
android:textColor="#000000"
android:textSize="16.0sp"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/dialog_search_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="@dimen/dp_10" />
</LinearLayout>
第二个问题是键盘弹出的时候,会出现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<PoiItem> 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) {
}
});
/**
* 监听edittext内容的变化,去搜索
*/
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<Tip> 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传回去就好了.网站的支持!
来源:http://blog.csdn.net/villa_mou/article/details/77704270
猜你喜欢
- 一、代理模式 代理模式是常用的java设计模式,特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息、过滤消息、把消息转发给委
- 前面我们讲到了Spring在进行事务逻辑织入的时候,无论是事务开始,提交或者回滚,都会触发相应的事务事件。本文首先会使用实例进行讲解Spri
- 说明本实例能够监控聚划算的抢购按钮,在聚划算整点聚的时间到达时自动弹开页面(URL自己定义)。可以自定义监控持续分钟数,同时还可以通过多线程
- @Transactional是我们在用Spring时候几乎逃不掉的一个注解,该注解主要用来声明事务。它的实现原理是通过Spring AOP在
- 什么是MybatisMyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software fou
- 第9版《Java核心技术卷Ⅰ》的第607页介绍了一个方法:Collections类中的sort方法可以对实现了List接口的集合进行排序。这
- 引言上一节讲面试中被问到分布式系统概念相关的,讲完了分布式系统的概念,优点缺点和 RPC 后,我以为这个问题就到此结束了,没想到成功给自己挖
- class文件中的特殊字符串首先说明一下, 所谓的特殊字符串出现在class文件中的常量池中,本着循序渐进和减少跨度的原则, 首先把clas
- 为什么需要 StreamStream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 Output
- 可以用于简单的过期订单取消支付、7天自动收货场景中1、Spring Boot整合redis 参考https://www.jb51.net/a
- SLF4J是一个日志框架抽象层,底下绑定具体的日志框架,比如说Log4J,Logback,Java Logging API等。SLF4J也有
- Nashorn是什么Nashorn,发音“nass-horn”,是德国二战时一个坦克的命名,同时也是java8新一代的javascript引
- summarydetail传统的Spring项目会有很多的配置文件,比如我们要使用Redis,一般除了对应的依赖的jar包我们还需要在app
- 1. 测试文档、期望达到的目标文档效果用于测试的Word文档如下所示,包含的空白段落影响文章整体布局及美观性:目标文档效果:2. 辅助工具2
- EntityWrapper的常用方法#WHERE (issue_type = ?) AND (status = ? OR status =
- 引言Spring Boot的一个便捷功能是外部化配置,可以轻松访问属性文件中定义的属性。本文将详细介绍@ConfigurationPrope
- 1.Shito简介1.1 什么是shiroApache Shiro是一个java安全(权限)框架Shiro可以非常容易的开发出足够好的应用,
- 关于Plupload的介绍,相信它的官网http://www.plupload.com/已经给得很详细了。Plupload的上传原理简单点说
- 前言用户注册功能是每一个系统的入口门面功能,很多人可能会以为很简单,不就是一个简单的CRUD吗?其实不然,要把前后端功能都做出来,页面跳转也
- @Cacheable自定义KeyGenerator1. 概述SpringBoot 使用 @Cacheable 可以方便的管理缓存数据,在不指