<?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
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="在" >
</TextView>
<EditText
android:id="@+id/city"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="北京" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="市内找" >
</TextView>
<AutoCompleteTextView
android:id="@+id/searchkey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.88"
android:text="餐厅" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal" >
<Button
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="12"
android:background="@drawable/button_style"
android:onClick="searchButtonProcess"
android:padding="10dip"
android:text="开始" />
<Button
android:id="@+id/map_next_data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="12"
android:background="@drawable/button_style"
android:onClick="goToNextPage"
android:padding="10dip"
android:text="下一组数据" />
</LinearLayout>
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.baidu.mapapi.map.SupportMapFragment" />
</LinearLayout>
/**
* 演示poi搜索功能
* 介绍关键词查询,suggestion查询和查看餐饮类place详情页功能
*/
public class PoiSearchDemo extends FragmentActivity implements
OnGetPoiSearchResultListener, OnGetSuggestionResultListener {
/**
* POI检索接口
* */
private PoiSearch mPoiSearch = null;
/**
* 建议查询接口
* */
private SuggestionSearch mSuggestionSearch = null;
private BaiduMap mBaiduMap = null;
/**
* 搜索关键字输入窗口
*/
private AutoCompleteTextView keyWorldsView = null;
private ArrayAdapter<String> sugAdapter = null;
private int load_Index = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_poisearch);
/**
* public static PoiSearch newInstance()
* 创建PoiSearch实例
* 返回:PoiSearch实例
* */
mPoiSearch = PoiSearch.newInstance();
/**
* public void setOnGetPoiSearchResultListener(OnGetPoiSearchResultListener listener)
* 设置poi检索监听者
* 参数:listener - 监听者
*
* 需要实现的方法:
* onGetPoiDetailResult(PoiDetailResult result)
* onGetPoiResult(PoiResult result)
* */
mPoiSearch.setOnGetPoiSearchResultListener(this);
/**
* public static SuggestionSearch newInstance()
* 获取建议检索实例
* 返回: 建议检索实例
* */
mSuggestionSearch = SuggestionSearch.newInstance();
/**
* public void setOnGetSuggestionResultListener(OnGetSuggestionResultListener listener)
* 设置建议请求结果监听器
* 参数:listener - 请求结果监听器
* 需要实现的方法:void onGetSuggestionResult(SuggestionResult result)
* */
mSuggestionSearch.setOnGetSuggestionResultListener(this);
keyWorldsView = (AutoCompleteTextView) findViewById(R.id.searchkey);
sugAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line);
keyWorldsView.setAdapter(sugAdapter);
mBaiduMap = ((SupportMapFragment) (getSupportFragmentManager().findFragmentById(R.id.map)))
.getBaiduMap();
/**
* 当输入关键字变化时,动态更新建议列表
*/
keyWorldsView.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;
}
String city = ((EditText) findViewById(R.id.city)).getText().toString();
/**
* 使用建议搜索服务获取建议列表,结果在onSuggestionResult()中更新
*/
/**
* public boolean requestSuggestion(SuggestionSearchOption option)
* 建议请求入口
* 参数:option - 请求参数
* 返回:成功发起检索返回true , 失败返回false
*
* public SuggestionSearchOption keyword(java.lang.String keyword)
* 指定建议关键字
* 参数:keyword - 关键字
* 返回: 该建议查询请求参数对象
*
* public SuggestionSearchOption city(java.lang.String city)
* 设置建议请求城市
* 参数:city - 城市
* 返回:该建议查询请求参数对象
* */
mSuggestionSearch.requestSuggestion((new SuggestionSearchOption())
.keyword(cs.toString()).city(city));
}
});
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
mPoiSearch.destroy();
/**
* 释放对象资源
* */
mSuggestionSearch.destroy();
super.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
/**
* 开始搜索按钮点击事件
*
* @param v
*/
public void searchButtonProcess(View v) {
//城市
EditText editCity = (EditText) findViewById(R.id.city);
//查询关键字
EditText editSearchKey = (EditText) findViewById(R.id.searchkey);
/**
* public boolean searchInCity(PoiCitySearchOption option)
* 城市内检索
* 参数:option - 请求参数
* 返回:成功发起检索返回true , 失败返回false
*
* PoiCitySearchOption:poi城市内检索参数
*
* public PoiCitySearchOption city(java.lang.String city)
* 检索城市
* 参数:city - 检索城市
* 返回:该poi城市内检索参数对象
*
* public PoiCitySearchOption keyword(java.lang.String key)搜索关键字
* 参数:key - 搜索关键字
* 返回:该poi城市内检索参数对象
*
* public PoiCitySearchOption pageNum(int pageNum)
* 分页编号
* 参数:pageNum - 分页编号
* 返回:该poi城市内检索参数对象
* */
mPoiSearch.searchInCity((new PoiCitySearchOption())
.city(editCity.getText().toString())
.keyword(editSearchKey.getText().toString())
.pageNum(load_Index));
}
/**
* 下一组数据按钮点击事件
* */
public void goToNextPage(View v) {
load_Index++;
searchButtonProcess(null);
}
OnGetPoiSearchResultListener/
public void onGetPoiResult(PoiResult result) {
if (result == null|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(PoiSearchDemo.this, "未找到结果", Toast.LENGTH_LONG).show();
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
mBaiduMap.clear();
/**
* PoiOverlay:用于显示poi的overly
* */
PoiOverlay overlay = new MyPoiOverlay(mBaiduMap);
/**
* 设置地图 Marker覆盖物点击事件监听者,点击覆盖物时自动调用MyPoiOverlay类的onPoiClick方法.
* */
mBaiduMap.setOnMarkerClickListener(overlay);
/**
* public void setData(PoiResult poiResult)
* 设置POI数据
* 参数:poiResult - 设置POI数据
*
* public final void addToMap()
* 将所有Overlay 添加到地图上
*
* public void zoomToSpan()
* 缩放地图,使所有Overlay都在合适的视野内
* 注: 该方法只对Marker类型的overlay有效
* */
overlay.setData(result);
overlay.addToMap();
overlay.zoomToSpan();
return;
}
/**
* AMBIGUOUS_KEYWORD
检索词有岐义
AMBIGUOUS_ROURE_ADDR
检索地址有岐义
KEY_ERROR
key有误
NO_ERROR
检索结果正常返回
NOT_SUPPORT_BUS
该城市不支持公交搜索
NOT_SUPPORT_BUS_2CITY
不支持跨城市公交
RESULT_NOT_FOUND
没有找到检索结果
ST_EN_TOO_NEAR
起终点太近
* */
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
// 当输入关键字在本市没有找到,但在其他城市找到时,返回包含该关键字信息的城市列表
String strInfo = "在";
/**
* public java.util.List<CityInfo> getSuggestCityList()
* 返回城市列表页的结果数
* 返回:城市列表页的结果数
*
* CityInfo:搜索结果城市信息。
* 搜索结果城市城市信息,包含城市名和该城市搜索结果数量
* */
for (CityInfo cityInfo : result.getSuggestCityList()) {
/**
* public java.lang.String city
* 城市名称
* */
strInfo += cityInfo.city;
strInfo += ",";
}
strInfo += "找到结果";
Toast.makeText(PoiSearchDemo.this, strInfo, Toast.LENGTH_LONG).show();
}
}
public void onGetPoiDetailResult(PoiDetailResult result) {
if (result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(PoiSearchDemo.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
} else {
/**
* public java.lang.String getName()
* 获取 poi 名称
* 返回:poi 名称
*
* public java.lang.String getAddress()
* 获取 poi 地址
* 返回:poi 地址
* */
Toast.makeText(PoiSearchDemo.this, result.getName() + ": " + result.getAddress(),
Toast.LENGTH_SHORT).show();
}
}
OnGetSuggestionResultListener/
/**
* void onGetSuggestionResult(SuggestionResult result)
* 建议查询结果回调函数
* 参数:result - 建议查询结果
* */
@Override
public void onGetSuggestionResult(SuggestionResult res) {
if (res == null || res.getAllSuggestions() == null) {
return;
}
sugAdapter.clear();
/**
* static class SuggestionResult.SuggestionInfo
* suggestion信息类
* */
for (SuggestionResult.SuggestionInfo info : res.getAllSuggestions()) {
/**
* key
* 联想词key
* */
if (info.key != null)
sugAdapter.add(info.key);
}
sugAdapter.notifyDataSetChanged();
}
private class MyPoiOverlay extends PoiOverlay {
public MyPoiOverlay(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public boolean onPoiClick(int index) {
super.onPoiClick(index);
/**
* public PoiResult getPoiResult()获取该 PoiOverlay 的 poi数据
*
* public java.util.List<PoiInfo> getAllPoi()
* 获取Poi检索结果
* 返回:Poi检索结果
* */
PoiInfo poi = getPoiResult().getAllPoi().get(index);
// if (poi.hasCaterDetails) {
/**
* public boolean searchPoiDetail(PoiDetailSearchOption option)
* POI 详情检索
* 参数:option - 请求参数
* 返回:成功发起检索返回true , 失败返回false
*
* PoiDetailSearchOption:poi 详情检索参数
*
* public PoiDetailSearchOption poiUid(java.lang.String poiUid)
* 欲检索的poi的uid
* 参数:poiUid - poi的uid
* 返回:该 poi 详情检索参数对象
*
* public java.lang.String uid
* poi id 如果为isPano为true,可用此参数 调用街景组件PanoramaService类的
* requestPanoramaWithPoiUId方法检索街景数据
* */
mPoiSearch.searchPoiDetail((new PoiDetailSearchOption()).poiUid(poi.uid));
// }
return true;
}
}
}