(转载请注明出处哦)具体的百度地图权限和apikey配置以及基础地图的配置不叙述,百度地图定位可以看这个链接的http://blog.csdn.net/heweigzf/article/details/51084358,先来看一波搜索需要的基本布局layout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<AutoCompleteTextView
android:id=
"@+id/autosearchresult"
android:background=
"@null"
android:hint=
"@string/please_input_addr"
android:layout_width=
"match_parent"
android:layout_height=
"@dimen/y30"
android:completionThreshold=
"2"
android:background=
"@drawable/edittext_shap"
android:singleLine=
"true"
/>
<ListView
android:id=
"@+id/poimsglist"
android:divider=
"@color/grey"
android:dividerHeight=
"1px"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
/>
|
AutoCompleteTextView可以换成EditTextView,都是可以的,既然是城市poi检索,就会有需要的城市名,可以是定位得到的也可以是传值过来的,这里我就以Intent传值的形式了,先初始化城市检索核心类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/**
* 城市内搜索
*/
private
void
citySearch(
int
page,String keyword,
int
pageCapacity) {
Intent intent=getIntent();
if
(intent!=
null
){
String city=intent.getStringExtra(
"city"
);
if
(city!=
null
){
mPoiSearch=PoiSearch.newInstance();
mPoiSearch.setOnGetPoiSearchResultListener(
this
);
// 监听检索结果回调
// 设置检索参数
PoiCitySearchOption citySearchOption =
new
PoiCitySearchOption();
citySearchOption.city(city);
// 城市
citySearchOption.keyword(keyword);
// 关键字
citySearchOption.pageCapacity(pageCapacity);
// 默认每页10条
citySearchOption.pageNum(page);
// 分页编号
// 发起检索请求
mPoiSearch.searchInCity(citySearchOption);
}
}
}
|
接下来就是运用以上的方法了,可以明显的看出来我们是通过输入框来自动检索,需要的给AutoCompleteTextView设置输入监听
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
autosearchresult.addTextChangedListener(
this
);
@Override
public
void
beforeTextChanged(CharSequence s,
int
start,
int
count,
int
after) {
}
@Override
public
void
onTextChanged(CharSequence s,
int
start,
int
before,
int
count) {
}
// 输入完成后自动进行检索调用以上citySearch方法
@Override
public
void
afterTextChanged(Editable s) {
String searchdialog=s.toString();
if
(searchdialog.length()>
1
){
citySearch(
0
, searchdialog,
20
);
}
}
|
检索成功后,以下为OnGetPoiSearchResultListener回调的检索结果,getAllPoi()包含所有的检索结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
private
ArrayList<String> addrs=
new
ArrayList<String>();
private
ArrayList<String> names=
new
ArrayList<String>();
private
ArrayList<LatLng> mlLatLngs=
new
ArrayList<LatLng>();
@Override
public
void
onGetPoiDetailResult(PoiDetailResult arg0) {
//获取Place详情页检索结果
}
@Override
public
void
onGetPoiResult(PoiResult arg0) {
//获取POI检索结果
if
(arg0.error!=SearchResult.ERRORNO.NO_ERROR){
//BaseApp.getInstance().showToast("检索fail");
}
else
{
if
(addrs!=
null
){
addrs.clear();
}
if
(mlLatLngs!=
null
){
mlLatLngs.clear();
}
if
(names!=
null
){
names.clear();
}
if
(arg0.getAllPoi()!=
null
&&arg0.getAllPoi().size()>
0
){
List<PoiInfo> mPoiInfos=arg0.getAllPoi();
for
(
int
i =
0
; i < mPoiInfos.size(); i++) {
names.add(mPoiInfos.get(i).name);
addrs.add(mPoiInfos.get(i).address);
mlLatLngs.add(mPoiInfos.get(i).location);
//对需要的信息设置适配器,如果想在其他界面用,可以自己创建回调接口
mSearchListAdapter=
new
SearchListAdapter(addrs, names, BaseApp.getInstance());
poiresultlist.setAdapter(mSearchListAdapter);
}
}
}
}
|
最后很重要的一步,要记得销毁poisearch对象,避免可能会报空指针异常
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@Override
public
void
onBackPressed() {
super
.onBackPressed();
if
(mPoiSearch!=
null
){
mPoiSearch.destroy();
}
}
@Override
public
void
onDestroy() {
super
.onDestroy();
if
(mPoiSearch!=
null
){
mPoiSearch.destroy();
}
}
|