Android 高德地圖之poi搜索功能的實現(xiàn)代碼
廢話不多說,先看效果,如果大家感覺不錯,請參考實現(xiàn)代碼

這個功能我是用Fragmentdialog里面做的,也遇到不少坑
第一,就是設(shè)置背景的drawable為純白色導(dǎo)致鍵盤彈出的時候,recyclerview的布局被頂上去導(dǎo)致出現(xiàn)白色布局,有點扎眼;最后改成了設(shè)置為和背景色一個顏色就和好了
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="請輸入關(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>
第二個問題是鍵盤彈出的時候,會出現(xiàn)dialog布局整體被頂上去
最后通過設(shè)置 style來解決
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//解決dialogfragment布局不被頂上去的方法
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar);
}
最后就是實現(xiàn)搜索功能了
第一個點擊搜索時,鍵盤和搜索按鈕兩個都是同樣的效果
/**
* 搜索功能
*/
private void searchLocationPoi() {
//關(guā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(), "", "");// 第一個參數(shù)表示搜索字符串,第二個參數(shù)表示poi搜索類型,第三個參數(shù)表示poi搜索區(qū)域(空字符串代表全國)
query.setPageSize(20);// 設(shè)置每頁最多返回多少條poiitem
query.setPageNum(0);// 設(shè)置查第一頁
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)聽EditText里面內(nèi)容的變化來搜索,其實也很簡單
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)聽edittext內(nèi)容的變化,去搜索
*/
private void textChangeSearch(CharSequence charSequence) {
String content = charSequence.toString().trim();//獲取自動提示輸入框的內(nèi)容
Logger.e(content);
InputtipsQuery inputtipsQuery = new InputtipsQuery(content, "");//初始化一個輸入提示搜索對象,并傳入?yún)?shù)
Inputtips inputtips = new Inputtips(getActivity(), inputtipsQuery);//定義一個輸入提示對象,傳入當(dāng)前上下文和搜索對象
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)聽,實現(xiàn)輸入提示的監(jiān)聽方法onGetInputtips()
inputtips.requestInputtipsAsyn();//輸入查詢提示的異步接口實現(xiàn)
}
ok,搞定,最后只需要搞個回調(diào),把Search后點擊的item傳回去就好了.希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Flutter中使用setState時的6個簡單技巧總結(jié)
平常在使用flutter的控件時我們都知道,要刷新頁面那么只需要調(diào)用setState()方法即可,這篇文章主要給大家介紹了關(guān)于Flutter中使用setState時的6個簡單技巧,需要的朋友可以參考下2022-05-05
Android使用Handler實現(xiàn)View彈性滑動
這篇文章主要介紹了Android使用Handler實現(xiàn)View彈性滑動,介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友可以參考下2016-08-08
Android 手機(jī)獲取手機(jī)號實現(xiàn)方法
本文主要介紹Android 獲取手機(jī)號的實現(xiàn)方法,這里提供了實現(xiàn)方法,和具體操作流程,并符實現(xiàn)代碼,有需要的小伙伴可以參考下2016-09-09
Android?NDK開發(fā)之FFmpeg視頻添加水印
這篇文章主要介紹了在Android?NDK開發(fā)中如何通過FFmpeg為視頻添加水印,文中的示例代碼講解詳細(xì),對我們了解Android開發(fā)有一定的幫助,感興趣的可以學(xué)習(xí)一下2021-12-12

