android 百度地图 在线建议查询,Android 百度地图 SDK v3_3_0 (五) ---POI搜索和在线建议查询功能...
目前百度地圖SDK所集成的檢索服務(wù)包括:POI檢索、公交信息查詢、線路規(guī)劃、地理編碼、在線建議查詢、短串分享。
本篇博客將先介紹POI檢索和在線建議查詢(在地圖地位功能基礎(chǔ)上實現(xiàn)的,還不知道定位的童靴,請參考Android
百度地圖 SDK v3.3.0 (二)--- 地圖定位和圖層展示)
百度地圖SDK提供三種類型的POI檢索:周邊檢索、區(qū)域檢索和城市內(nèi)檢索。下面將以城市內(nèi)檢索為例,向大家介紹如何使用檢索服務(wù)。
在介紹檢索服務(wù)之前,想上一張要實現(xiàn)的效果圖:
好了!現(xiàn)在我們上代碼,來實現(xiàn)上面的功能(代碼中都做了相應(yīng)的注解)
1.創(chuàng)建POI和在線建議查詢實例
/**
* 初始化搜索模塊,注冊搜索事件監(jiān)聽
*/
private void initPOIListener() {
//POI檢索實例
mPoiSearch = PoiSearch.newInstance();
//創(chuàng)建POI檢索監(jiān)聽者
mPoiSearch.setOnGetPoiSearchResultListener(this);
//聯(lián)想詞檢索實例
mSuggestionSearch = SuggestionSearch.newInstance();
//聯(lián)想詞檢索監(jiān)聽者
mSuggestionSearch.setOnGetSuggestionResultListener(this);
}
2.創(chuàng)建POI檢索監(jiān)聽者,并做相關(guān)的事件處理
@Override
public void onGetSuggestionResult(SuggestionResult res) {
if (res == null || res.getAllSuggestions() == null) {
return;
}
sugAdapter.clear();
for (SuggestionResult.SuggestionInfo info : res.getAllSuggestions()) {
if (info.key != null)
sugAdapter.add(info.key);
}
sugAdapter.notifyDataSetChanged();
}
@Override
public void onGetPoiDetailResult(PoiDetailResult result) {
// 未找到了結(jié)果
if (result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(MainActivity.this, "抱歉,未找到結(jié)果", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(MainActivity.this,
result.getName() + ": " + result.getAddress(),
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onGetPoiResult(PoiResult result) {
// 未找到結(jié)果
if (result == null
|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(MainActivity.this, "未找到結(jié)果", Toast.LENGTH_LONG)
.show();
return;
}
// 結(jié)果沒有異常,找到了結(jié)果
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
mBaiduMap.clear();
PoiOverlay overlay = new MyPoiOverlay(mBaiduMap);
mBaiduMap.setOnMarkerClickListener(overlay);
overlay.setData(result);
overlay.addToMap();
overlay.zoomToSpan();
return;
}
// 當輸入關(guān)鍵字在本市沒有找到,但在其他城市找到時,返回包含該關(guān)鍵字信息的城市列表
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
String strInfo = "在";
for (CityInfo cityInfo : result.getSuggestCityList()) {
strInfo += cityInfo.city;
strInfo += ",";
}
strInfo += "找到結(jié)果";
Toast.makeText(MainActivity.this, strInfo, Toast.LENGTH_LONG)
.show();
}
}
/**
* 提供了在地圖上標識搜索結(jié)果的方法
*
* @author TanZuAi
*
*/
private class MyPoiOverlay extends PoiOverlay {
public MyPoiOverlay(BaiduMap baiduMap) {
super(baiduMap);
}
@Override
public boolean onPoiClick(int index) {
super.onPoiClick(index);
// 獲取poi覆蓋物的詳細信息
PoiInfo poi = getPoiResult().getAllPoi().get(index);
// uid是POI檢索中獲取的POI ID信息
mPoiSearch.searchPoiDetail((new PoiDetailSearchOption())
.poiUid(poi.uid));
return true;
}
}
3.設(shè)置按鈕的相關(guān)事件。
/**
* 影響搜索按鈕點擊事件
*
* @param v
*/
public void searchButtonProcess(View v) {
EditText editCity = (EditText) findViewById(R.id.city);// 輸入的城市
EditText editSearchKey = (EditText) findViewById(R.id.searchkey);// 輸入的關(guān)鍵詞
// 發(fā)起檢索請求
mPoiSearch.searchInCity((new PoiCitySearchOption())
.city(editCity.getText().toString())// 根據(jù)城市
.keyword(editSearchKey.getText().toString())// 根據(jù)關(guān)鍵字
.pageNum(load_Index));// 查詢的頁數(shù)
}
/**
* 每添加一頁進行查詢
*
* @param v
*/
public void goToNextPage(View v) {
load_Index++;
searchButtonProcess(null);
}? ? ? ? ? 4.為了節(jié)省電量和資源,得設(shè)置搜索的相關(guān)生命周期
@Override
protected void onDestroy() {
mPoiSearch.destroy();
mSuggestionSearch.destroy();
// 在activity執(zhí)行onDestroy時執(zhí)行mMapView.onDestroy(),實現(xiàn)地圖生命周期管理
mMapView.onDestroy();
super.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
// 在activity執(zhí)行onResume時執(zhí)行mMapView. onResume (),實現(xiàn)地圖生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
// 在activity執(zhí)行onPause時執(zhí)行mMapView. onPause (),實現(xiàn)地圖生命周期管理
mMapView.onPause();
}
好了!代碼實現(xiàn)已經(jīng)介紹完畢!相信大家都看的懂!有什么不懂的或者建議,可以在下面留言!
下面是本篇博客的源碼下載地址:
原文:http://blog.csdn.net/tanzuai/article/details/43835431
總結(jié)
以上是生活随笔為你收集整理的android 百度地图 在线建议查询,Android 百度地图 SDK v3_3_0 (五) ---POI搜索和在线建议查询功能...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 开发卫星菜单,andro
- 下一篇: android sina oauth2.