室内Po检索
界面设计,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/seach_ll"
android:orientation="horizontal" >
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="@drawable/search"/>
<EditText
android:id="@+id/indoorsearch_content_et"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@null"
android:layout_height="40dp"
android:hint="在当前室内搜索" >
<!--<requestFocus />-->
</EditText>
<View
android:layout_width="1px"
android:layout_height="30dp"
android:background="#9C9C9C"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/indoorsearch_search_tv"
android:layout_width="60dp"
android:layout_gravity="center"
android:textSize="20dp"
android:gravity="center"
android:layout_height="wrap_content"
android:text="搜索" />
</LinearLayout>
<Button
android:id="@+id/indoorRoutePlane"
android:layout_width="wrap_content"
android:layout_below="@+id/seach_ll"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="室内路线规划" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/seach_ll"
android:layout_weight="1"
android:id="@+id/viewStub">
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:background="@drawable/circleshape"
android:layout_alignParentBottom="true"
android:id="@+id/indoorsearch_path_bt"
android:layout_marginBottom="40dp"
android:text="路线" />
</RelativeLayout>
</RelativeLayout>
其中,circleshape.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<!-- 填充的颜色 -->
<solid android:color="#3FB9FA" />
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="15dip" />
<!-- padding:Button里面的文字与Button边界的间隔 -->
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
界面效果如图:
activity继承这几个接口
implements BaiduMap.OnBaseIndoorMapListener, OnGetPoiSearchResultListener
默认定位到北京南站
LatLng centerpos = new LatLng(39.871281,116.385306); // 北京南站
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(centerpos).zoom(19.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
创建检索当前建筑物室内POI检索对象
mPoiSearch = PoiSearch.newInstance();
设置POI检索监听者
mPoiSearch.setOnGetPoiSearchResultListener(this);
搜索按钮的监听
indoorsearch_content_et = (EditText) findViewById(R.id.indoorsearch_content_et);
indoorsearch_search_tv = findViewById(R.id.indoorsearch_search_tv);
indoorsearch_search_tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MapBaseIndoorMapInfo indoorInfo = mBaiduMap.getFocusedBaseIndoorMapInfo();
if (indoorInfo == null) {
Toast.makeText(IndoorSearchActivity.this, "当前无室内图,无法搜索" , Toast.LENGTH_LONG).show();
return;
}
//设置检索参数
PoiIndoorOption option = new PoiIndoorOption().poiIndoorBid(
indoorInfo.getID()).poiIndoorWd(indoorsearch_content_et.getText().toString());
//发起检索请求
mPoiSearch.searchPoiIndoor( option);
//隐藏软键盘
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
});
设置监听事件来监听进入和移出室内图
@Override
public void onBaseIndoorMapMode(boolean b, MapBaseIndoorMapInfo mapBaseIndoorMapInfo) {
if (b) {
stripView.setVisibility(View.VISIBLE);
if (mapBaseIndoorMapInfo == null) {
return;
}
mFloorListAdapter.setmFloorList(mapBaseIndoorMapInfo.getFloors());
stripView.setStripAdapter(mFloorListAdapter);
} else {
stripView.setVisibility(View.GONE);
}
mMapBaseIndoorMapInfo = mapBaseIndoorMapInfo;
}
poi检索结果返回
@Override
public void onGetPoiResult(PoiResult poiResult) {
}
@Override
public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
}
@Override
public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {
}
@Override
public void onGetPoiIndoorResult(PoiIndoorResult result) {
mBaiduMap.clear();
poiIndoorInfoList.clear();
if (result == null || result.error == PoiIndoorResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(IndoorSearchActivity.this, "无结果" , Toast.LENGTH_LONG).show();
return;
}
if (result.error == PoiIndoorResult.ERRORNO.NO_ERROR) {
IndoorPoiOverlay overlay = new MyIndoorPoiOverlay(mBaiduMap);
poiIndoorInfoList=result.getmArrayPoiInfo();
indoorsearch_rv.setVisibility(View.VISIBLE);
indoorsearch_path_bt.setVisibility(View.GONE);
inDoorSearchAdapter=new InDoorSearchAdapter(poiIndoorInfoList,this,this);
indoorsearch_rv.setLayoutManager(new LinearLayoutManager(this));
indoorsearch_rv.setAdapter(inDoorSearchAdapter);
Log.d("zjccc", poiIndoorInfoList.get(1).address);
mBaiduMap.setOnMarkerClickListener(overlay);
//address
overlay.setData(result);
overlay.addToMap();
overlay.zoomToSpan();
}
}
注:使用poi检索的时候一定要记得将官方demo中的assets目录下拷贝到自己工程,不然汇报maker's icon can not be null这个错误,
当然你也可以修改设置maker图片的方法,可以用自己的图片
到此,已经可以实现室内poi检索,效果如图: