Android百度地图之云检索功能
jopen
11年前
下面介绍百度Demo提供的最后一个功能:云检索功能。该示例主要为海量位置数据的存储、检索、展示提供了一种解决方案。里面用的是测试的ak,如果开发者要使用自己的数据,则需要使用自己申请的ak。代码如下:
云检索功能介绍类(CloudSearchDemo):
package com.home; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class CloudSearchDemo extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cloud_search_demo); } public void startCloudSearchDemo(View view) { Intent intent = new Intent(); intent.setClass(this, CloudSearchActivity.class); startActivity(intent); } }
CloudSearchDemo对应的布局XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="36dp" android:text="@string/cloud_search_tips" android:textColor="@android:color/black" android:textSize="16sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="32dp" android:onClick="startCloudSearchDemo" android:text="开始体验" /> </RelativeLayout>
string/cloud_search_tips:
<string name="cloud_search_tips"> \t\tLBS.云是百度地图针对LBS开发者推出的平台级服务。结合已有的地图API和SDK服务。通过开放服务端存储和计算能力,提供海量位置数据存储、检索、展示一体化解决方案。\n\n \t\t该服务对开发者免费开放。测试demo里写入了测试的ak。开发者可以使用测试ak查看 LBS.云检索的效果。如果开发者要使用自己的数据,请在申请ak后替换demo中的ak。\n\n \t\t如有任何关于LBS.云服务的问题,诸如如何申请ak、如何存储和检索数据等,请访问百度地图官方“LBS开放平台”。地址:http://lbsyun.baidu.com/ \n </string>
云检索主Activity:
package com.home; import java.util.List; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; import android.widget.Toast; import com.baidu.mapapi.BMapManager; import com.baidu.mapapi.cloud.Bounds; import com.baidu.mapapi.cloud.BoundsSearchInfo; import com.baidu.mapapi.cloud.CustomPoiInfo; import com.baidu.mapapi.cloud.DetailResult; import com.baidu.mapapi.cloud.DetailSearchInfo; import com.baidu.mapapi.cloud.GeoSearchListener; import com.baidu.mapapi.cloud.GeoSearchManager; import com.baidu.mapapi.cloud.GeoSearchResult; import com.baidu.mapapi.cloud.NearbySearchInfo; import com.baidu.mapapi.cloud.RegionSearchInfo; import com.baidu.mapapi.map.ItemizedOverlay; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.OverlayItem; import com.baidu.platform.comapi.basestruct.GeoPoint; public class CloudSearchActivity extends Activity implements GeoSearchListener { private MapView mMapView; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); DemoApplication app = (DemoApplication) this.getApplication(); if (app.mBMapManager == null) { app.mBMapManager = new BMapManager(this); app.mBMapManager.init(DemoApplication.strKey, new DemoApplication.MyGeneralListener()); } setContentView(R.layout.lbs_search); GeoSearchManager.getInstance().init(CloudSearchActivity.this); mMapView = (MapView) findViewById(R.id.bmapView); mMapView.getController().enableClick(true); mMapView.getController().setZoom(12); mMapView.setDoubleClickZooming(true); } @Override public void onGetGeoDetailsResult(DetailResult result, int type, int iError) { if (result != null) { if (result.content != null) { Toast.makeText(CloudSearchActivity.this, result.content.name, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(CloudSearchActivity.this, "status:" + result.status, Toast.LENGTH_SHORT).show(); } } } @Override public void onGetGeoResult(GeoSearchResult result, int type, int iError) { if (result != null && result.poiList != null && result.poiList.size() > 0) { CloudOverlay poiOverlay = new CloudOverlay(this, mMapView); poiOverlay.setData(result.poiList); mMapView.getOverlays().clear(); mMapView.getOverlays().add(poiOverlay); mMapView.refresh(); mMapView.getController().animateTo( new GeoPoint((int) (result.poiList.get(0).latitude * 1e6), (int) (result.poiList.get(0).longitude * 1e6))); } } @Override protected void onPause() { mMapView.onPause(); super.onPause(); } @Override protected void onResume() { mMapView.onResume(); super.onResume(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mMapView.onSaveInstanceState(outState); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); mMapView.onRestoreInstanceState(savedInstanceState); } @Override protected void onDestroy() { mMapView.destroy(); super.onDestroy(); } public void click(View v) { if (v.getId() == R.id.regionSearch) { RegionSearchInfo r = new RegionSearchInfo(); r.queryWords = "北京市五中"; r.ak = "3307f6443b2bdcae21af5d9564b5a88c"; r.cityName = "北京"; r.filter.put("databox", 848); r.scope = 2; GeoSearchManager.getInstance().searchRegion(r); } if (v.getId() == R.id.nearbySearch) { NearbySearchInfo r = new NearbySearchInfo(); r.queryWords = "北京"; r.ak = "3307f6443b2bdcae21af5d9564b5a88c"; r.location = new GeoPoint(39956948, 116412214); r.radius = 10000000; r.filter.put("databox", 848); r.scope = 2; GeoSearchManager.getInstance().searchNearby(r); } if (v.getId() == R.id.boundsSearch) { BoundsSearchInfo r = new BoundsSearchInfo(); r.queryWords = "五中"; r.ak = "3307f6443b2bdcae21af5d9564b5a88c"; r.bounds = new Bounds(39843895, 116402214, 40956948, 116431457); r.filter.put("databox", 848); r.scope = 2; GeoSearchManager.getInstance().searchBounds(r); } if (v.getId() == R.id.detailsSearch) { DetailSearchInfo r = new DetailSearchInfo(); r.id = 81217; r.ak = "3307f6443b2bdcae21af5d9564b5a88c"; r.scope = 2; GeoSearchManager.getInstance().searchDetail(r); } } } class CloudOverlay extends ItemizedOverlay { List<CustomPoiInfo> mLbsPoints; Activity mContext; public CloudOverlay(Activity context, MapView mMapView) { super(null, mMapView); mContext = context; } public void setData(List<CustomPoiInfo> lbsPoints) { if (lbsPoints != null) { mLbsPoints = lbsPoints; } for (CustomPoiInfo rec : mLbsPoints) { GeoPoint pt = new GeoPoint((int) (rec.latitude * 1e6), (int) (rec.longitude * 1e6)); OverlayItem item = new OverlayItem(pt, rec.name, rec.address); Drawable marker1 = this.mContext.getResources().getDrawable( R.drawable.icon_marka); item.setMarker(marker1); addItem(item); } } @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } @Override protected boolean onTap(int arg0) { CustomPoiInfo item = mLbsPoints.get(arg0); Toast.makeText(mContext, item.name, Toast.LENGTH_SHORT).show(); return super.onTap(arg0); } }
其布局文件:
<?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:orientation="horizontal" > <Button android:id="@+id/regionSearch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="本地搜索" /> <Button android:id="@+id/nearbySearch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="周边搜索" /> <Button android:id="@+id/boundsSearch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="区域搜索" /> <Button android:id="@+id/detailsSearch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" 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>
配置文件和Application类与之前的一样。
附上图片效果:
来自:http://blog.csdn.net/u010142437/article/details/11664781