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


猜你喜欢
- 来自同事_Smile的封装应用,记录学习一下,分享给大家,具体如下:1、AnimImageViewLoader类的封装/** * 适用于an
- 稳定度(稳定性)一个排序算法是稳定的,就是当有两个相等记录的关键字R和S,且在原本的列表中R出现在S之前,在排序过的列表中R也将会是在S之前
- java 遍历listpackage com.tiandy.core.rest;import java.util.ArrayList;imp
- 前言在SpringBoot中,对于JavaBean的属性一般都绑定在配置文件中,比如application.properties/appli
- 今天突发奇想,想做一个智能拼图游戏来给哄女友。需要实现这些功能第一图片自定义第二宫格自定义,当然我一开始就想的是3*3 4*4 5*5,没有
- 前言Flutter (Channel stable, 2.10.3, on Microsoft Windows [Version 10.0.
- 本文研究的主要是JVM中的flag设置详解的相关内容,具体介绍如下。一、堆大小设置-Xmx3550m:设置JVM最大可用内存为3550M。-
- 本文实例讲述了C#通过指针实现快速拷贝的方法。分享给大家供大家参考。具体实现方法如下:// fastcopy.cs// 编译时使用:/uns
- 当项目有中多个线程,如何查找死锁?最近,在IDEA上进行多线程编程中老是在给线程加锁的时候,总是会遇到死锁问题,而当程序出现死锁问题时,编译
- 指纹识别作为最新兴起的用户身份验证机制,已经被越来越多的应用程序所采用,相比传统的密码九宫格等验证方法,指纹识别更加安全,如今越来越多的安卓
- 一. 引用的概念引用不是新定义一个变量,而是给已存在变量取了一个别名,编译器不会为引用变量开辟内存空间,它和它引用的变量共用同一块内存空间。
- 本实例为大家分享了Android实现短信验证码自动填写功能,供大家参考,具体内容如下实现思路很简单:1、在需要输入验证码的Activity代
- 什么是自旋锁说道自旋锁就要从多线程下的锁机制说起,由于在多处理器系统环境中有些资源因为其有限性,有时需要互斥访问(mutual exclus
- 网上的解决方法:这个是从网上看来的file-->setting-->plugins,搜索tomcat然后install之后会提示
- 本文实例讲述了Android仿英语流利说取词放大控件的实现方法。分享给大家供大家参考,具体如下:1 取词放大控件英语流利说是一款非常帮的口语
- Java常用类包装类由于Java语言中的基本类型不是面向对象,并不具备对象的性质,实际使用存在很多不便。Java在java.lang包中提供
- 这篇文章主要介绍了SpringBoot文件访问映射如何实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- 多表联合查询resultType的返回值一般数据按参数类型返回<select id="queryCarIdList"
- 前言本文是我之前写的这篇文章《Android图文混排-实现EditText图文混合插入上传》的升级版,除了在EditText实现了图片上传之
- 本文实例讲述了Android编程之手机壁纸WallPaper设置方法。分享给大家供大家参考,具体如下:/** * Andorid设置手机屏幕