在android项目中要实现加载baidu地图,其前提条件是其jar包BaiduLBS_Android.jar,下面对其步骤进行说明
1:加载baidu视图
MapView mMapView = (MapView) view.findViewById(R.id.bmapView);
2:获取地图控件引用
BaiduMap mBaiduMap = mMapView.getMap();
3:设置地图类型
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
4:设置中心点
LatLng cenpt = new LatLng(26.086177,119.260795);
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(cenpt));
5:设置Marker坐标点,这个点用于在地图上的坐标
LatLng point = new LatLng(Double.parseDouble(“26.786177”,“119.260795”);
6:构建Marker图标
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_location_off)
7:构建MarkerOption,用于在地图上添加Marker
Bundle args = new Bundle(); //这个用于在所画的点上附加相关信息
args.putString("siteId", site.getSiteId());
args.putString("siteName", site.getSiteName());
// 构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions().position(point).extraInfo(args).icon(bitmap);
8:在地图上添加所需要画的点
mBaiduMap.addOverlay(option);
如果需要点击“点”,以显示数据,则在mBaiduMap上增加点击事件,下面的代码是点击后,从服务端获取数据,并显示的代码段
mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker arg0) {
Bundle args = arg0.getExtraInfo();
getSiteDataById(args.get("siteId").toString(),args.get("siteName").toString());
return false;
}
private void getSiteDataById(String id,final String siteName) {
RequestParams params = new RequestParams("id", id);
httpClient.get(Constants.URL_GET_SITE_BY_ID,params, new AsyncHttpResponseHandler(){
@Override
public void onFinish() {
super.onFinish();
loadingDialog.dismiss();
}
@Override
public void onStart() {
super.onStart();
loadingDialog.show();
}
@Override
public void onSuccess(String content) {
super.onSuccess(content);
Gson gson = new Gson();
Type listType = new TypeToken<Response<List<SiteValueInfo>>>() {}.getType();
Response<List<SiteValueInfo>> response = gson.fromJson(content, listType);
List<SiteValueInfo> factors = response.getDatas();
if(factors == null)
return;
StringBuffer sb = new StringBuffer();
if(factors.size() > 0){
String time = new SimpleDateFormat(Constants.DATE_FORMAT).format(new Date(Long.parseLong(factors.get(0).getPicktime())));
sb.append("采集时间:"+time+"\n");
}
for (SiteValueInfo factor : factors) {
sb.append(factor.getFactorName()+":"+factor.getValue()+" "+factor.getUnit()+"\n");
}
new AlertDialog.Builder(getActivity()).setTitle(siteName).setMessage(sb.toString()).setNegativeButton("确定", null).show();
};
});
}
});
LatLng cenpt = new LatLng(Double.parseDouble(sites.get(0).getLatitude()),Double.parseDouble(sites.get(0).getLongitude()));
mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(cenpt));
MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.zoomTo(11);
mBaiduMap.animateMapStatus(mapStatusUpdate);