android 百度地图 在线建议查询,Android 百度地图 SDK v3_3_0 (五) ---POI搜索和在线建议查询功能...
目前百度地圖SDK所集成的檢索服務包括:POI檢索、公交信息查詢、線路規劃、地理編碼、在線建議查詢、短串分享。
本篇博客將先介紹POI檢索和在線建議查詢(在地圖地位功能基礎上實現的,還不知道定位的童靴,請參考Android
百度地圖 SDK v3.3.0 (二)--- 地圖定位和圖層展示)
百度地圖SDK提供三種類型的POI檢索:周邊檢索、區域檢索和城市內檢索。下面將以城市內檢索為例,向大家介紹如何使用檢索服務。
在介紹檢索服務之前,想上一張要實現的效果圖:
好了!現在我們上代碼,來實現上面的功能(代碼中都做了相應的注解)
1.創建POI和在線建議查詢實例
/**
* 初始化搜索模塊,注冊搜索事件監聽
*/
private void initPOIListener() {
//POI檢索實例
mPoiSearch = PoiSearch.newInstance();
//創建POI檢索監聽者
mPoiSearch.setOnGetPoiSearchResultListener(this);
//聯想詞檢索實例
mSuggestionSearch = SuggestionSearch.newInstance();
//聯想詞檢索監聽者
mSuggestionSearch.setOnGetSuggestionResultListener(this);
}
2.創建POI檢索監聽者,并做相關的事件處理
@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) {
// 未找到了結果
if (result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(MainActivity.this, "抱歉,未找到結果", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(MainActivity.this,
result.getName() + ": " + result.getAddress(),
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onGetPoiResult(PoiResult result) {
// 未找到結果
if (result == null
|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(MainActivity.this, "未找到結果", Toast.LENGTH_LONG)
.show();
return;
}
// 結果沒有異常,找到了結果
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;
}
// 當輸入關鍵字在本市沒有找到,但在其他城市找到時,返回包含該關鍵字信息的城市列表
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
String strInfo = "在";
for (CityInfo cityInfo : result.getSuggestCityList()) {
strInfo += cityInfo.city;
strInfo += ",";
}
strInfo += "找到結果";
Toast.makeText(MainActivity.this, strInfo, Toast.LENGTH_LONG)
.show();
}
}
/**
* 提供了在地圖上標識搜索結果的方法
*
* @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.設置按鈕的相關事件。
/**
* 影響搜索按鈕點擊事件
*
* @param v
*/
public void searchButtonProcess(View v) {
EditText editCity = (EditText) findViewById(R.id.city);// 輸入的城市
EditText editSearchKey = (EditText) findViewById(R.id.searchkey);// 輸入的關鍵詞
// 發起檢索請求
mPoiSearch.searchInCity((new PoiCitySearchOption())
.city(editCity.getText().toString())// 根據城市
.keyword(editSearchKey.getText().toString())// 根據關鍵字
.pageNum(load_Index));// 查詢的頁數
}
/**
* 每添加一頁進行查詢
*
* @param v
*/
public void goToNextPage(View v) {
load_Index++;
searchButtonProcess(null);
}? ? ? ? ? 4.為了節省電量和資源,得設置搜索的相關生命周期
@Override
protected void onDestroy() {
mPoiSearch.destroy();
mSuggestionSearch.destroy();
// 在activity執行onDestroy時執行mMapView.onDestroy(),實現地圖生命周期管理
mMapView.onDestroy();
super.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
// 在activity執行onResume時執行mMapView. onResume (),實現地圖生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
// 在activity執行onPause時執行mMapView. onPause (),實現地圖生命周期管理
mMapView.onPause();
}
好了!代碼實現已經介紹完畢!相信大家都看的懂!有什么不懂的或者建議,可以在下面留言!
下面是本篇博客的源碼下載地址:
原文:http://blog.csdn.net/tanzuai/article/details/43835431
總結
以上是生活随笔為你收集整理的android 百度地图 在线建议查询,Android 百度地图 SDK v3_3_0 (五) ---POI搜索和在线建议查询功能...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 开发卫星菜单,andro
- 下一篇: cuda8.0.44linux.run,