百度云服务 怎么生成自己的数据(二)

来源:互联网 发布:vs2017 windows.h 编辑:程序博客网 时间:2024/04/16 22:00

(二)百度云检索

开发者在完成数据的LBS云端存储之后,便可调用SDK云检索服务,检索自己存储在云端的数据。具体方法如下:

首先实例化GeoSearchListener接口:

源码复制打印关于
  1. // 为 CloudManager 注册 CloudListener 监听者  
  2. CloudManager.getInstance().init(CloudSearchActivity.this);  
  3. public void onGetDetailSearchResult(DetailSearchResult result, int error) {  
  4.     if (result != null) {  
  5.         if (result.poiInfo != null) {  
  6.             Toast.makeText(  
  7.                 CloudSearchActivity.this,   
  8.                 result.poiInfo.title,  
  9.                 Toast.LENGTH_SHORT  
  10.             ).show();  
  11.         }  
  12.         else{  
  13.             Toast.makeText(  
  14.                 CloudSearchActivity.this,   
  15.                 "status:" + result.status,   
  16.                 Toast.LENGTH_SHORT  
  17.             ).show();  
  18.         }  
  19.     }  
  20. }  
  21.   
  22. public void onGetSearchResult(CloudSearchResult result, int error) {  
  23.     if (result != null && result.poiList!= null && result.poiList.size() > 0) {  
  24.         CloudOverlay poiOverlay = new CloudOverlay(this,mMapView);  
  25.         poiOverlay.setData(result.poiList);  
  26.         mMapView.getOverlays().clear();  
  27.         mMapView.getOverlays().add(poiOverlay);  
  28.         mMapView.refresh();  
  29.         mMapView.getController().animateTo(  
  30.             new GeoPoint((int)(result.poiList.get(0).latitude * 1e6),  
  31.             (int)(result.poiList.get(0).longitude * 1e6))  
  32.         );  
  33.     }  
  34. }  

发起云检索请求:

源码复制打印关于
  1. LocalSearchInfo info = new LocalSearchInfo();  
  2. //注意:此处填写的ak为服务端的ak,并不是SDK的key,详细信息请查看LBS云检索的官方文档  
  3. info.ak = "B266f735e43ab207ec152deff44fec8b";  
  4. info.geoTableId = 31869;  
  5. info.tags = "";  
  6. info.q="天安门";  
  7. info.region = "北京市";  
  8. CloudManager.getInstance().localSearch(info);  

运行结果如下:

lbs_cloud.png


这是百度给的运行案例,有点不全,不过可以自己到官网下载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

0 0
原创粉丝点击