1搜索服務
百度地圖SDK集成搜索服務包括:位置檢索、周邊檢索、範圍檢索、公交檢索、駕乘檢索、步行檢索,通過初始化MKSearch類,註冊搜索結果的監聽對象MKSearchListener,實現異步搜索服務。首先自定義MySearchListener實現MKSearchListener接口,通過不同的回調方法,獲得搜索結果:
public class MySearchListener implements MKSearchListener {
@Override
public void onGetAddrResult(MKAddrInfo result, int iError) {
//返回地址信息搜索結果
}
@Override
public void onGetDrivingRouteResult(MKDrivingRouteResult result, int iError) {
//返回駕乘路線搜索結果
}
@Override
public void onGetPoiResult(MKPoiResult result, int type, int iError) {
//返回poi搜索結果
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult result, int iError) {
//返回公交搜索結果
}
@Override
public void onGetWalkingRouteResult(MKWalkingRouteResult result, int iError) {
//返回步行路線搜索結果
}
@Override
public void onGetBusDetailResult(MKBusLineResult result, int iError) {
//返回公交車詳情信息搜索結果
}
@Override
public void onGetSuggestionResult(MKSuggestionResult result, intiError) {
//返回聯想詞信息搜索結果
}
}
在MyMapActivity中添加成員變量:
MKSearch mMKSearch = null;
然後在onCreate()中初始化:
mMKSearch = new MKSearch();
mMKSearch.init(mBMapMan, new MySearchListener());//注意,MKSearchListener只支持一個,以最後一次設置爲準
2興趣點(poi)搜索
2.1 範圍檢索
指在給定的一個矩形區域內,根據開發者設定的指定關鍵字,搜索興趣點信息,所使用的方法爲:poiSearchInbounds(String key, GeoPoint ptLB, GeoPoint ptRT);核心代碼如下:
如要檢索北京西站與北京北站爲頂點所確定的距形區域內的KFC餐廳,使用以下代碼發起檢索:
// 北京西站
GeoPoint ptLB = new GeoPoint( (int)(39.901375 * 1E6),(int)(116.329099 * 1E6));
// 北京北站
GeoPoint ptRT = new GeoPoint( (int)(39.949404 * 1E6),(int)(116.360719 * 1E6));
mMKSearch.poiSearchInbounds("KFC", ptLB, ptRT);
Tips:想知道某個興趣點的百度地圖座標嗎?
請移步百度地圖座標拾取系統http://api.map.baidu.com/lbsapi/getpoint/index.html
2.2 城市檢索
城市檢索,即在某一城市內搜索興趣點信息。所使用的方法是:poiSearchInCity(String city, String key);核心代碼如下:
如要檢索北京的KFC餐廳,使用以下代碼發起檢索:
mMKSearch.poiSearchInCity("北京", "KFC");
2.3 周邊檢索
周邊檢索指的是以指定座標點爲圓心,根據給定關鍵字查詢一定半徑範圍內的全部興趣點。使用方法:poiSearchNearBy(String key, GeoPoint pt, int radius);核心代碼如下:
檢索天安門周邊5000米之內的KFC餐廳:
mMKSearch.poiSearchNearBy("KFC", new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)), 5000);
2.4 展示搜索結果
實現MySearchListener的onGetPoiResult,並展示檢索結果:
@Override
public void onGetPoiResult(MKPoiResult res, int type, int error) {
// 錯誤號可參考MKEvent中的定義
if ( error == MKEvent.ERROR_RESULT_NOT_FOUND){
Toast.makeText(MyMapActivity.this, "抱歉,未找到結果",Toast.LENGTH_LONG).show();
return ;
}
else if (error != 0 || res == null) {
Toast.makeText(MyMapActivity.this, "搜索出錯啦..", Toast.LENGTH_LONG).show();
return;
}
// 將poi結果顯示到地圖上
PoiOverlay poiOverlay = new PoiOverlay(MyMapActivity.this, mMapView);
poiOverlay.setData(res.getAllPoi());
mMapView.getOverlays().clear();
mMapView.getOverlays().add(poiOverlay);
mMapView.refresh();
//當ePoiType爲2(公交線路)或4(地鐵線路)時, poi座標爲空
for(MKPoiInfo info : res.getAllPoi() ){
if ( info.pt != null ){
mMapView.getController().animateTo(info.pt);
break;
}
}
}
運行結果如下圖所示:
3 地址信息查詢
根據地理座標查詢地址信息:
mMKSearch.reverseGeocode(new GeoPoint(40057031, 116307852)); //逆地址解析
mMKSearch.geocode(key, city);//地址解析
reverseGeocode返回結果在MKSearchListener裏的onGetAddrResult方法,核心代碼如下所示:
public void onGetAddrResult(MKAddrInfo res, int error) {
if (error != 0) {
String str = String.format("錯誤號:%d", error);
Toast.makeText(MyMapActivity.this, str, Toast.LENGTH_LONG).show();
return;
}
mMapView.getController().animateTo(res.geoPt);
String strInfo = String.format("緯度:%f 經度:%f\r\n", res.geoPt.getLatitudeE6()/1e6,res.geoPt.getLongitudeE6()/1e6);
Toast.makeText(MyMapActivity.this, strInfo, Toast.LENGTH_LONG).show();
}
geocode返回結果在MKSearchListener裏的onGetPoiResult方法,核心代碼如下:
public void onGetPoiResult(MKPoiResult res, int type, int error) {
if (error != 0 || res == null) {
Toast.makeText(MyMapActivity.this, "解析失敗", Toast.LENGTH_LONG).show();
return;
}
if (res != null&&res.getCurrentNumPois() > 0) {
GeoPointptGeo = res.getAllPoi().get(0).pt; // 移動地圖到該點:
mMapView.getController().animateTo(ptGeo);
String strInfo = String.format("緯度:%f 經度:%f\r\n", ptGeo.getLatitudeE6()/1e6,ptGeo.getLongitudeE6()/1e6);
strInfo += "\r\n附近有:";
for (int i = 0; i <res.getAllPoi().size(); i++) {
strInfo += (res.getAllPoi().get(i).name + ";");
}
Toast.makeText(MyMapActivity.this, strInfo, Toast.LENGTH_LONG).show();
}
}
4 在線建議查詢
根據關鍵詞查詢在線建議詞,具體使用的方法爲:suggestionSearch(String key),參數key爲關鍵字;獲取查詢結果的方法需要實現MKSearchListener接口中的onGetSuggestionResult方法,核心代碼如下所示:
ListView mSuggestionList = (ListView) findViewById(R.id.listView1);
@Override
public void onGetSuggestionResult(MKSuggestionResult res, int iError){
if (iError!= 0 || res == null) {
Toast.makeText(MyMapActivity.this, "抱歉,未找到結果", Toast.LENGTH_LONG).show();
return;
}
int nSize = res.getSuggestionNum();
String[] mStrSuggestions = new String[nSize];
for (int i = 0; i <nSize; i++){
mStrSuggestions[i] = res.getSuggestion(i).city + res.getSuggestion(i).key;
}
ArrayAdapter<String> suggestionString = new ArrayAdapter<String>(MyMapActivity.this, android.R.layout.simple_list_item_1,mStrSuggestions);
mSuggestionList.setAdapter(suggestionString);
}
更多詳細信息請登錄百度地圖API官方網站:http://developer.baidu.com/map/
百度地圖API論壇:http://bbs.lbsyun.baidu.com/