Android BaiduMapSug检索 即关键字匹配

2020-11-16  本文已影响0人  懵懵懂懂_YOYO

注意:Sug检索,city为必填项。

Sug检索默认不限制在city内,检索结果优先展示city内结果。

地点检索输入提示检索(Sug检索)

地点检索输入提示服务(也被称为POI热词建议检索、在线建议检索、Suggestion POI search),简称Sug检索,是指根据关键词查询在线建议词。为了帮助开发者实现检索出来的关键词快速定位到地图上,SDK开放了检索结果的经纬度信息及对应POI点的UID信息。

Sug检索示例

布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="50dip" android:background="@color/dkgray" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="在" android:textColor="@color/white" android:textStyle="bold" /> <EditText android:id="@+id/city" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="北京" android:textColor="@color/white" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="市内找" android:textColor="@color/white" android:textStyle="bold" /> <AutoCompleteTextView android:id="@+id/searchkey" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.88" android:hint="请输入联想关键字" android:textColor="@color/white" /> </LinearLayout> <ListView android:id="@+id/sug_list" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>

1.

// 初始化建议搜索模块,注册建议搜索事件监听

mSuggestionSearch = SuggestionSearch.newInstance();

mSuggestionSearch.setOnGetSuggestionResultListener(this);

2.

/**

* 获取在线建议搜索结果,得到requestSuggestion返回的搜索结果

*

* @param suggestionResult    Sug检索结果

*/

@Override

public void onGetSuggestionResult(SuggestionResult suggestionResult) {

if (suggestionResult ==null || suggestionResult.getAllSuggestions() ==null) {

return;

    }

List> suggest =new ArrayList<>();

    for (SuggestionResult.SuggestionInfo info : suggestionResult.getAllSuggestions()) {

if (info.getKey() !=null && info.getDistrict() !=null && info.getCity() !=null) {

HashMap map =new HashMap<>();

            map.put("key",info.getKey());

            map.put("city",info.getCity());

            map.put("dis",info.getDistrict());

            suggest.add(map);

        }

}

SimpleAdapter simpleAdapter =new SimpleAdapter(getApplicationContext(),

            suggest,

            R.layout.item_layout,

            new String[]{"key", "city","dis"},

            new int[]{R.id.sug_key, R.id.sug_city, R.id.sug_dis});

    mSugListView.setAdapter(simpleAdapter);

    simpleAdapter.notifyDataSetChanged();

}

3.

// 当输入关键字变化时,动态更新建议列表

mKeyWordsView.addTextChangedListener(new TextWatcher() {

@Override

    public void afterTextChanged(Editable arg0) {

}

@Override

    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

}

@Override

    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

if (cs.length() <=0) {

return;

        }

// 使用建议搜索服务获取建议列表,结果在onSuggestionResult()中更新

        mSuggestionSearch.requestSuggestion((new SuggestionSearchOption())

.keyword(cs.toString())// 关键字

                .city(mEditCity.getText().toString())); // 城市

    }

});

4.释放检索实例

@Override

protected void onDestroy() {

super.onDestroy();

    mSuggestionSearch.destroy();

}

参考:地点输入提示检索

代码截图:

上一篇下一篇

猜你喜欢

热点阅读