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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JavaFx实现拼图游戏
![](https://img.aspxhome.com/file/2023/4/69714_0s.jpg)
Java 将List中的实体类按照某个字段进行分组并存放至Map中操作
![](https://img.aspxhome.com/file/2023/0/82300_0s.jpg)
使用springboot aop来实现读写分离和事物配置
![](https://img.aspxhome.com/file/2023/6/69086_0s.jpg)
详解java基础--提示对话框的使用
![](https://img.aspxhome.com/file/2023/2/80902_0s.jpg)
java读取word-excel-ppt文件代码
Android GestureDetector实现手势滑动效果
![](https://img.aspxhome.com/file/2023/1/87331_0s.jpg)
java8中:: 用法示例(JDK8双冒号用法)
Spring Boot中lombok的安装与使用详解
![](https://img.aspxhome.com/file/2023/5/66055_0s.jpg)
java用接口、多态、继承、类计算三角形和矩形周长及面积的方法
Unity实战之FlyPin(见缝插针)小游戏的实现
![](https://img.aspxhome.com/file/2023/1/69201_0s.jpg)
浅谈java8中map的新方法--replace
如何从dump文件中提取出C#源代码
![](https://img.aspxhome.com/file/2023/4/91284_0s.png)
Java Code Cache满导致应用性能降低问题解决
MyBatis-Plus实现2种分页方法(QueryWrapper查询分页和SQL查询分页)
![](https://img.aspxhome.com/file/2023/1/92561_0s.png)
Java 十大排序算法之冒泡排序刨析
![](https://img.aspxhome.com/file/2023/4/68134_0s.png)
Java使用EasyExcel动态添加自增序号列
![](https://img.aspxhome.com/file/2023/4/75474_0s.png)
android6.0权限动态申请框架permissiondispatcher的方法
spring cloud gateway 限流的实现与原理
![](https://img.aspxhome.com/file/2023/8/73068_0s.png)
Android Rsa数据加解密的介绍与使用示例
![](https://img.aspxhome.com/file/2023/3/94973_0s.png)