android百度地图开发总结

来源:互联网 发布:孕妇尿常规检查数据 编辑:程序博客网 时间:2024/06/09 20:26

项目近期开发加入百度地图的需求,初次使用遇到一些基础问题,总结如下:百度地图的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();
StringCity = "北京";geoCoder.geocode(new GeoCodeOption().city(City).address(City));//编码城市获取坐标
Activity实现OnGetGeoCoderResultListener接口有如下方法:@Overridepublic void onGetGeoCodeResult(GeoCodeResult arg0) {   LatLng lng = arg0.getLocation();//获取编码城市坐标;}@Overridepublic 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 0
原创粉丝点击