百度地图SDK集成搜索服务
百度地图SDK集成搜索服务包括:位置检索、周边检索、范围检索、公交检索、驾乘检索、步行检索、短串分享,通 过初始化MKSearch类, 注册搜索结果的监听对象MKSearchListener,实现异步搜索服务。检索服务使用完成之后, 需要调用MKSearch的destory()方法来释放资源。
1.1 范围检索
指在给定的一个矩形区域内,根据开发者设定的指定关键字,搜索兴趣点信息,所使用的方法为:poiSearchInbounds(String key, GeoPoint ptLB, GeoPoint ptRT);
1.2 城市检索
城市检索,即在某一城市内搜索兴趣点信息。所使用的方法是:poiSearchInCity(String city, String key);
1.3 周边检索
周边检索指的是以指定坐标点为圆心,根据给定关键字查询一定半径范围内的全部兴趣点。使用方法:poiSearchNearBy(String key, GeoPoint pt, int radius);
java的代码如下:
public class BaseMapActivity extends Activity {
private MKSearch mMKSearch = new MKSearch();// 地图的搜索
private RadioGroup poiRadioGroup;
private RadioButton poiRadioButton01,poiRadioButton02,poiRadioButton03;
private EditText etKeyWord;
private Button searchButton;
private Toast mToast;
private BMapManager mBMapManager;// 地图的管理器,必须在setContentView之前就完成初始化
private MapView mMapView = null;// 地图控件
private MapController mMapController = null;// 地图控制器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBMapManager = new BMapManager(this);// 地图的管理器,必须在setContentView之前就完成初始化
mBMapManager.init("25f9e5484032fa551ecbdb2838bec0d7",
new MKGeneralListener() {
public void onGetPermissionState(int is) {
if (is == MKEvent.ERROR_PERMISSION_DENIED)
Log.e("wo", "API Key错误");
}
@Override
public void onGetNetworkState(int is) {
if (is == MKEvent.ERROR_NETWORK_CONNECT)
Log.e("wo", "网络错误");
}
});
// 加载布局
setContentView(R.layout.activity_main);
mMapView = (MapView) findViewById(R.id.bmapView);
searchButton = (Button) findViewById(R.id.searchButton);
poiRadioGroup = (RadioGroup) findViewById(R.id.poiRadioGroup);
etKeyWord = (EditText) findViewById(R.id.etKeyWord);
poiRadioButton01=(RadioButton) findViewById(R.id.poiRadioButton01);
poiRadioButton02=(RadioButton) findViewById(R.id.poiRadioButton02);
poiRadioButton03=(RadioButton) findViewById(R.id.poiRadioButton03);
mMapController = mMapView.getController();// 得到mMapView的控制权,可以用它控制和驱动平移和缩放
mMapController.enableClick(true);// 设置可点击
mMapController.setZoom(12);// 设置地图的zoom级别
mMapView.setBuiltInZoomControls(true);// 设置启用内置的缩放控件
// 用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)-->北京东城区
GeoPoint point = new GeoPoint((int) (39.915 * 1E6),
(int) (116.404 * 1E6));
mMapController.setCenter(point);// 设置地图中心点
mMapController.setZoom(12);// 设置地图zoom级别
mMapView.regMapViewListener(mBMapManager, new MKMapViewListener() {// 地图的状态改变监听
@Override
public void onMapMoveFinish() {
showToast("题图移动完毕");
}
@Override
public void onMapLoadFinish() {
showToast("地图载入完毕");
}
@Override
public void onMapAnimationFinish() {
}
@Override
public void onGetCurrentMap(Bitmap arg0) {
}
@Override
public void onClickMapPoi(MapPoi arg0) {
if (arg0 != null) {
showToast(arg0.strText);// 点击显示地址
}
}
});
// **************新加的搜索功能**************
// 注意,MKSearchListener只支持一个,以最后一次设置为准
mMKSearch.init(mBMapManager, new MySearchListener());
String str = etKeyWord.getText().toString();
poiRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==R.id.poiRadioButton01){
// 检索北京西站与北京北站为顶点所确定的距形区域内的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);
}else if(checkedId==R.id.poiRadioButton02){
//检索北京的KFC餐厅
mMKSearch.poiSearchInCity("北京", "KFC");
}else if(checkedId==R.id.poiRadioButton03){
//检索天安门周边5000米之内的KFC餐厅
mMKSearch.poiSearchNearBy("KFC", new GeoPoint((int) (39.915 * 1E6),
(int) (116.404 * 1E6)), 5000);
}
}
});
searchButton.setOnClickListener(new View.OnClickListener() {// 搜索按钮点击监听
@Override
public void onClick(View arg0) {
}
});
}
@Override
protected void onResume() {
// MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onResume()
mMapView.onResume();
super.onResume();
}
@Override
protected void onPause() {
// MapView的生命周期与Activity同步,当activity挂起时需调用MapView.onPause()
mMapView.onPause();
super.onPause();
}
@Override
protected void onDestroy() {
// MapView的生命周期与Activity同步,当activity销毁时需调用MapView.destroy()
mMapView.destroy();
// 退出应用调用BMapManager的destroy()方法
if (mBMapManager != null) {
mBMapManager.destroy();
mBMapManager = null;
}
super.onDestroy();
}
private void showToast(String msg) {
if (mToast == null) {
mToast = Toast.makeText(this, msg, 500);
} else {
mToast.setText(msg);
mToast.setDuration(500);
}
mToast.show();
}
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 res, int type, int error) {
// 返回poi搜索结果
// 错误号可参考MKEvent中的定义
if (error == MKEvent.ERROR_RESULT_NOT_FOUND) {
Toast.makeText(BaseMapActivity.this, "抱歉,未找到结果",
Toast.LENGTH_LONG).show();
return;
} else if (error != 0 || res == null) {
Toast.makeText(BaseMapActivity.this, "搜索出错啦..",
Toast.LENGTH_LONG).show();
return;
}
// 将poi结果显示到地图上
PoiOverlay poiOverlay = new PoiOverlay(BaseMapActivity.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;
}
}
}
@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 onGetShareUrlResult(MKShareUrlResult result, int type,
int error) {
// 在此处理短串请求返回结果.
}
@Override
public void onGetPoiDetailSearchResult(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
// 返回联想词信息搜索结果
}
}
}
xml的代码如下:
<?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
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#D1EEEE"
android:orientation="horizontal">
<RadioGroup
android:id="@+id/poiRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/poiRadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="范围检索"/>
<RadioButton
android:id="@+id/poiRadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="城市检索"/>
<RadioButton
android:id="@+id/poiRadioButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="周边检索"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#eaeaea"
android:gravity="center" >
<EditText
android:id="@+id/etKeyWord"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:hint="请输入关键字"
android:padding="10dp" />
<Button
android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="查询" />
</LinearLayout>
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
</LinearLayout>
manifest.xml
那里的就和之前的一样,
有需要的朋友,可以看看我之前写的demo.
哈哈。
运行的结果如下截图: