Android 高德地圖之poi搜索功能的實(shí)現(xiàn)代碼
廢話不多說(shuō),先看效果,如果大家感覺(jué)不錯(cuò),請(qǐng)參考實(shí)現(xiàn)代碼

這個(gè)功能我是用Fragmentdialog里面做的,也遇到不少坑
第一,就是設(shè)置背景的drawable為純白色導(dǎo)致鍵盤(pán)彈出的時(shí)候,recyclerview的布局被頂上去導(dǎo)致出現(xiàn)白色布局,有點(diǎn)扎眼;最后改成了設(shè)置為和背景色一個(gè)顏色就和好了
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="請(qǐng)輸入關(guān)鍵字"
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>
第二個(gè)問(wèn)題是鍵盤(pán)彈出的時(shí)候,會(huì)出現(xiàn)dialog布局整體被頂上去
最后通過(guò)設(shè)置 style來(lái)解決
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//解決dialogfragment布局不被頂上去的方法
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar);
}
最后就是實(shí)現(xiàn)搜索功能了
第一個(gè)點(diǎn)擊搜索時(shí),鍵盤(pán)和搜索按鈕兩個(gè)都是同樣的效果
/**
* 搜索功能
*/
private void searchLocationPoi() {
//關(guān)閉鍵盤(pán)
KeyBoardUtils.closeKeybord(poiSearchInMaps, BaseApplication.mContext);
if (TextUtils.isEmpty(poiSearchInMaps.getText().toString().trim())) {
ToastUtils.showToastCenter("內(nèi)容為空!");
} else {
query = new PoiSearch.Query(poiSearchInMaps.getText().toString().trim(), "", "");// 第一個(gè)參數(shù)表示搜索字符串,第二個(gè)參數(shù)表示poi搜索類型,第三個(gè)參數(shù)表示poi搜索區(qū)域(空字符串代表全國(guó))
query.setPageSize(20);// 設(shè)置每頁(yè)最多返回多少條poiitem
query.setPageNum(0);// 設(shè)置查第一頁(yè)
poiSearch = new PoiSearch(getActivity(), query);
poiSearch.setOnPoiSearchListener(this);
poiSearch.searchPOIAsyn();
}
}
然后回調(diào)中進(jìn)行處理
@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);
}
}
還有就是監(jiān)聽(tīng)EditText里面內(nèi)容的變化來(lái)搜索,其實(shí)也很簡(jiǎn)單
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) {
}
});
/**
* 監(jiān)聽(tīng)edittext內(nèi)容的變化,去搜索
*/
private void textChangeSearch(CharSequence charSequence) {
String content = charSequence.toString().trim();//獲取自動(dòng)提示輸入框的內(nèi)容
Logger.e(content);
InputtipsQuery inputtipsQuery = new InputtipsQuery(content, "");//初始化一個(gè)輸入提示搜索對(duì)象,并傳入?yún)?shù)
Inputtips inputtips = new Inputtips(getActivity(), inputtipsQuery);//定義一個(gè)輸入提示對(duì)象,傳入當(dāng)前上下文和搜索對(duì)象
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);
}
}
});//設(shè)置輸入提示查詢的監(jiān)聽(tīng),實(shí)現(xiàn)輸入提示的監(jiān)聽(tīng)方法onGetInputtips()
inputtips.requestInputtipsAsyn();//輸入查詢提示的異步接口實(shí)現(xiàn)
}
ok,搞定,最后只需要搞個(gè)回調(diào),把Search后點(diǎn)擊的item傳回去就好了.希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
android studio library 模塊中正確引用aar的實(shí)例講解
下面小編就為大家分享一篇android studio library 模塊中正確引用aar的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android主項(xiàng)目與Module中R類的區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于Android主項(xiàng)目與Module中R類的區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-02-02
Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)攔截讀取功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)攔截讀取功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android編程滑動(dòng)效果之倒影效果實(shí)現(xiàn)方法(附demo源碼下載)
這篇文章主要介紹了Android編程滑動(dòng)效果之倒影效果實(shí)現(xiàn)方法,基于繼承BaseAdapter自定義Gallery和ImageAdapter實(shí)現(xiàn)倒影的功能,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-02-02
Android中Activity生命周期和啟動(dòng)模式詳解
這篇文章主要介紹了Activity生命周期和啟動(dòng)模式詳解的相關(guān)資料,需要的朋友可以參考下2016-07-07
Android利用ZXing掃描二維碼的實(shí)例代碼解析
這篇文章主要介紹了Android利用ZXing掃描二維碼的實(shí)例解析,代碼簡(jiǎn)單易懂,非常不錯(cuò),需要的朋友可以參考下2016-12-12
Android界面 NotificationManager使用Bitmap做圖標(biāo)
Android界面 NotificationManager使用Bitmap做圖標(biāo),如何實(shí)現(xiàn)呢,本文將介紹解決方法,需要的朋友可以參考下2012-12-12

