android百度地图开发总结

	项目近期开发加入百度地图的需求,初次使用遇到一些基础问题,总结如下:
	百度地图的KEY申请地址http://lbsyun.baidu.com/,基础使用直接看百度地图API就可以,有Demo
	百度地图显示的时候全是网格没有地图,基本肯定就是KEY的问题。
1、BaiduMapOptions
BaiduMapOptions compassEnabled(boolean enabled) //设置是否允许指南针,默认允许。
BaiduMapOptions mapStatus(MapStatus status)             //设置地图初始化时的地图状态, 默认地图中心点								为北京天安门,缩放级别为 12.0f
BaiduMapOptions mapType(int mapType)                  //设置地图模式,默认普通地图
BaiduMapOptions overlookingGesturesEnabled(boolean enabled) //设置是否允许俯视手势,默认允许
BaiduMapOptions rotateGesturesEnabled(boolean enabled)        //设置是否允许旋转手势,默认允许
BaiduMapOptions scaleControlEnabled(boolean enabled)      //设置是否显示比例尺控件
BaiduMapOptions scrollGesturesEnabled(boolean enabled)        //设置是否允许拖拽手势,默认允许 
BaiduMapOptions zoomControlsEnabled(boolean enabled)      //设置是否显示缩放控件
BaiduMapOptions zoomGesturesEnabled(boolean enabled)      //设置是否允许缩放手势 

2、UiSettings

MapView mMapView = (MapView)findViewById(R.id.mapview);
BaiduMap mBaiduMap = mMapView.getMap();
UiSettings uiSettings = mBaiduMap.getUiSettings();
//设置是否允许旋转手势
uiSettings.setRotateGesturesEnabled(false);
//设置是否允许指南针
uiSettings.setCompassEnabled(false);
//设置是否允许俯视手势
uiSettings.setOverlookingGesturesEnabled(false);
//设置是否允许拖拽手势
uiSettings.setScrollGesturesEnabled(true);
//设置是否允许缩放手势
uiSettings.setZoomGesturesEnabled(true);
//隐藏地图上百度地图logo图标
mMapView.removeViewAt(1);
//隐藏地图上比例尺
mMapView.showScaleControl(true);
//隐藏缩放按钮
mMapView.showZoomControls(false);
3、根据坐标和级别缩放地图
//地图级别3-2000km
//地图级别4-1000km
//地图级别5-500km
//地图级别6-200km
//地图级别7-100km
//地图级别8-50km
//地图级别9-25km
//地图级别10-20km
//地图级别11-10km
//地图级别12-5km
//地图级别13-2km
//地图级别14-1km
//地图级别15-500m
//地图级别16-200m
//地图级别17-100m
//地图级别18-50m
//地图级别19-20m
//地图级别20-10m

private void scalePosition(LatLng position,int level){
   MapStatus.Builder builder = new MapStatus.Builder();
   if (level != 0) {
      builder.zoom(level);
   }
   if (position != null) {
      builder.target(position);
   }
   MapStatus mapStatus = builder.build();
       MapStatusUpdate statusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
   if (mBaiduMap!=null) {
      mBaiduMap.animateMapStatus(statusUpdate);//动画缩放,不用动画会直接刷新界面
   }
}

4、根据城市名称获取城市中心坐标或根据坐标反编码
/**
 * 地理编码查询接口
 */
private GeoCoder geoCoder = GeoCoder.newInstance();

String	City = "北京";
geoCoder.geocode(new GeoCodeOption().city(City).address(City));//编码城市获取坐标

Activity实现OnGetGeoCoderResultListener接口有如下方法:

@Override
public void onGetGeoCodeResult(GeoCodeResult arg0) {
   LatLng lng = arg0.getLocation();//获取编码城市坐标;
}

@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) {
   // 反编码
}

5、在地图上构建标志
//构建图片标志
BitmapDescriptor bitmap = BitmapDescriptorFactory
      .fromResource(R.drawable.worksite_map_feidianshang);
MarkerOptions option = new MarkerOptions().position(point)
      .icon(bitmap).zIndex(9).extraInfo(bundle);//bundle可以携带一些区分标志的信息等
try {
   mBaiduMap.addOverlay(option);
} catch (Exception e) {
   e.printStackTrace();
}
if (bitmap != null) {
   bitmap.recycle();
   bitmap = null;
}

//构建自定义布局标志

这个用的时候小心,有个bug
//4.4以下版本,空指针异常 
View view = LayoutInflater.from(mContext).inflate(R.layout.worksite_marker_quxian, null);
BitmapDescriptor bitmap = BitmapDescriptorFactory.fromView(view);//view不为空但报空指针异常
//解决办法:
//1、最外层要使用LinearLayout包住,才会不报错,我在第一次使用的RelativeLayout包住报错.
//2view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);


View view = LayoutInflater.from(mContext).inflate(
      R.layout.worksite_marker_quxian, null);
params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(params);

TextView markerNum = (TextView) view
      .findViewById(R.id.worksiteNum);
TextView cityName = (TextView) view
      .findViewById(R.id.cityName);
cityName.setVisibility(View.VISIBLE);
cityName.setText(info.get(i).RegionName);
markerNum.setText(count + "");
BitmapDescriptor bitmap = BitmapDescriptorFactory
      .fromView(view);
option = new MarkerOptions().position(point)
      .icon(bitmap).zIndex(9).extraInfo(bundle);
try {
   mBaiduMap.addOverlay(option);
} catch (Exception e) {
   e.printStackTrace();
}
if (bitmap != null) {
   bitmap.recycle();
   bitmap = null;
}

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值