(二)百度云检索
开发者在完成数据的LBS云端存储之后,便可调用SDK云检索服务,检索自己存储在云端的数据。具体方法如下:
首先实例化GeoSearchListener接口:
发起云检索请求:
运行结果如下:
这是百度给的运行案例,有点不全,不过可以自己到官网下载demo
我自己生成的云存储运用接下来介绍下。其实很简单
public class MainActivity extends Activity implements CloudListener{
MapController mapController;
BMapManager bMapManager;
MKSearch mkSearch;
MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bMapManager = new BMapManager(getApplication());
bMapManager.init("your key", null);//这里是用地图申请的key,不是ak切记
setContentView(R.layout.activity_main);
mapView = (MapView)findViewById(R.id.map_View);
mapView.setTraffic(true);
mapView.setBuiltInZoomControls(true);
//到这里是基本的地图调用
//接下来就是百度云检索,自己设置相关的方法
CloudManager.getInstance().init(MainActivity.this);
NearbySearchInfo info = new NearbySearchInfo();
info.ak = "r5hDaNARq1cT76aR36lvHChh";
info.geoTableId = 58953;
info.location = "116.403689,39.914957";
info.radius = 30000;
CloudManager.getInstance().nearbySearch(info);
}
@Override
public void onGetDetailSearchResult(DetailSearchResult result, int arg1) {
if (result != null) {
if (result.poiInfo != null) {
Toast.makeText(MainActivity.this, result.poiInfo.title, Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "status:" + result.status, Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onGetSearchResult(CloudSearchResult result, int arg1) {
if (result != null && result.poiList!= null && result.poiList.size() > 0) {
CloudOverlay poiOverlay = new CloudOverlay(this,mapView);
poiOverlay.setData(result.poiList);
mapView.getOverlays().clear();
mapView.getOverlays().add(poiOverlay);
mapView.refresh();
mapView.getController().animateTo(new GeoPoint((int)(result.poiList.get(0).latitude * 1e6), (int)(result.poiList.get(0).longitude * 1e6)));
}
}
class CloudOverlay extends ItemizedOverlay<OverlayItem>{
List<CloudPoiInfo> mlbs;
Context mcontext;
PopupOverlay popu;
public CloudOverlay( Context context,MapView mapview) {
super(null, mapview);
mcontext=context;
popu=new PopupOverlay(mapview, new PopupClickListener() {
@Override
public void onClickedPopup(int arg0) {
// TODO Auto-generated method stub
}
});
}
public void setData(List<CloudPoiInfo> lbs){
if(lbs!=null){
mlbs=lbs;
}
for(CloudPoiInfo rec:mlbs){
GeoPoint point = new GeoPoint((int)(rec.latitude*1e6),(int) (rec.longitude*1e6));
OverlayItem item = new OverlayItem(point, rec.title, rec.address);
Drawable mark= this.mcontext.getResources().getDrawable(R.drawable.icon_marka);
item.setMarker(mark);
addItem(item);
}
}
//这里是点击图层的事件响应,我做了个弹出图层
protected boolean onTap(int index) {
CloudPoiInfo item = mlbs.get(index);
// Toast.makeText(mcontext, item.title,Toast.LENGTH_LONG).show();
View pop =LayoutInflater.from(mcontext).inflate(R.layout.marker_pop, null);
TextView chen =(TextView)pop.findViewById(R.id.text_pop);
chen.setText(item.title);
GeoPoint pt = new GeoPoint((int)(item.latitude*1e6), (int)(item.longitude*1e6));
popu.showPopup(convertViewToBitMap(pop), pt, 32);
return super.onTap(index);
}
private Bitmap convertViewToBitMap(View v) {
// 启用绘图缓存
v.setDrawingCacheEnabled(true);
// 调用下面这个方法非常重要,如果没有调用这个方法,得到的bitmap为null
v.measure(MeasureSpec.makeMeasureSpec(210, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(120, MeasureSpec.EXACTLY));
// 这个方法也非常重要,设置布局的尺寸和位置
v.layout(0, 0, v.getMeasuredWidth() +20, v.getMeasuredHeight());
// 获得绘图缓存中的Bitmap
v.buildDrawingCache();
return v.getDrawingCache();
}
}
}
想要相关的代码可以到这下载哦
http://download.csdn.net/detail/chenaini119/7202189