我正在为中国客户创建一个 Android 应用程序,他们需要地图集成,因此 Google 地图不是一个选择,因为所有 Google 服务在中国都被屏蔽。我正在尝试使用百度地图,它被称为百度LBS(基于位置的服务)云。
获得没有叠加层的基本地图相对容易。该过程描述如下 http://developer.baidu.com/map/wiki/index.php?title=androidsdk/guide/hellobaidumap(中文,但如果您不懂该语言,代码就不言而喻)。下载最新的百度 Android SDK(撰写本文时为 v3.2.0)并将其作为库集成到我的 Eclipse 项目中没有问题,但不要太相信该链接中的文档,即使它是官方文档。他们的示例通常包含甚至无法编译的代码。例如,.jar 文件的名称与您在屏幕截图中看到的完全不同。
哦,他们的 .jar 库也被混淆了,使用起来非常烦人:-(
我需要注册一个百度帐户并去他们的控制中心生成密钥。要为移动设备创建访问密钥(“ak”),您需要输入对您的应用程序进行签名的密钥库的 SHA1 指纹,后跟清单中指定的包名称。
然后我将生成的密钥添加到标签下的清单中
<meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="xxx...xxx" />
然后,我从他们的示例项目 Cloud Search Activity 中复制了代码,因为我有想要显示的特定坐标。我实现了云监听器接口,如下所示:
@Override
public void onGetSearchResult(final CloudSearchResult result, final int error)
{
Log.w("onGetSearchResult", "status=" + result.status + ". size=" + result.size + ". total=" + result.total + ". error=" + error);
if(null != result && null != result.poiList && 0 < result.poiList.size())
{
mBaiduMap.clear();
final BitmapDescriptor bitmapDescriptor=BitmapDescriptorFactory.fromResource(R.drawable.icon_address_grey);
LatLng latitudeLongitude;
LatLngBounds.Builder builder=new Builder();
for(final CloudPoiInfo info : result.poiList)
{
latitudeLongitude=new LatLng(info.latitude, info.longitude);
final OverlayOptions overlayOptions=new MarkerOptions().icon(bitmapDescriptor).position(latitudeLongitude);
mBaiduMap.addOverlay(overlayOptions);
builder.include(latitudeLongitude);
}
final LatLngBounds bounds=builder.build();
MapStatusUpdate mapStatusUpdate=MapStatusUpdateFactory.newLatLngBounds(bounds);
mBaiduMap.animateMapStatus(mapStatusUpdate);
}
}
我添加了代码来启动查询(也从他们的示例项目复制):
@Override
public View onCreateView(final LayoutInflater layoutInflater, final ViewGroup viewGroup,
final Bundle savedInstanceState)
{
// initialize needs to be called
SDKInitializer.initialize(getApplication());
CloudManager.getInstance().init(MyFragment.this);
view=(ViewGroup)layoutInflater.inflate(R.layout.fragment_map, viewGroup, false);
mMapView=(MapView)view.findViewById(R.id.baiduMapView);
mBaiduMap=mMapView.getMap();
NearbySearchInfo info=new NearbySearchInfo();
info.ak="xxx...xxx";
info.geoTableId=12345;
info.tags="";
info.radius=30000;
info.location="116.403689,39.914957";
CloudManager.getInstance().nearbySearch(info);
return view;
}
不幸的是,我不断从服务器获得状态值 102(根据这个API页面 http://developer.baidu.com/map/android_refer/com/baidu/mapapi/cloud/BaseSearchResult.html这意味着STATUS_CODE_SECURITY_CODE_ERROR
。现在我不知道该怎么办。
我不明白的事情:
- 为什么在构建查询时需要重复访问密钥(“ak”)?在清单中包含一次还不够吗?
- 查询中的“geoTableId”值应该是什么?
有任何想法吗?