美文网首页
记录android百度地图的一些细节功能的实现。

记录android百度地图的一些细节功能的实现。

作者: 背锅TV丶伴奏大师 | 来源:发表于2018-07-30 11:15 被阅读66次

1.初始化地图比例尺的大小

先看一下百度地图API缩放级别和比例尺的大小关系。
百度地图API一共分为19级,比例尺分别为:

[1:20米(简称20米,后同),50米,100米,200米,500米,1公里,2公里,5公里,10公里,20公里,25公里,50公里,100公里,200公里,500公里,1000公里,2000公里,5000公里,10000公里]

分别对应:

[19级,18级,17级,16级,15级,14级,13级,12级,11级,10级,9级,8级,7级,6级,5级,4级,3级,2级,1级]

//在定义的定位监听的回调方法中
public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }                                   
            LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());
            MapStatus.Builder builder = new MapStatus.Builder();
            //在此处设置初始化的比例尺大小13.0f代表13级对应2km
            builder.target(ll).zoom(13.0f);
              mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            
        }

        public void onReceivePoi(BDLocation poiLocation) {
        }
    }

2.设置地图缩放按钮的摆放位置

//这是控制缩放控件的位置
mMapView.getChildAt(2).setPadding(0,0,width-200,height-400);

3.去掉百度地图的logo

// 删除百度地图LoGo
 mMapView.removeViewAt(1);

4.显示当前位置的mapview

// 开启定位图层
      mBaiduMap.setMyLocationEnabled(true);
// 定位初始化
      mLocClient = new LocationClient(this);
      mLocClient.registerLocationListener(myListener);
      LocationClientOption option = new LocationClientOption();
      option.setOpenGps(true); // 打开gps
      option.setCoorType("bd09ll"); // 设置坐标类型
      option.setScanSpan(1000);
      mLocClient.setLocOption(option);
      mLocClient.start();

5.在地图上定义自己的覆盖物

// 初始化全局 bitmap 信息,不用时及时 recycle
private Marker mMarkerA;
BitmapDescriptor bdA = BitmapDescriptorFactory.fromResource(R.mipmap.icon_gcoding);
//初始化覆盖物
private void initOverlay() {
    LatLng llA = new LatLng(30.287995, 120.122244);
    MarkerOptions ooA = new MarkerOptions().position(llA).icon(bdA)
                .zIndex(9).draggable(true);
    ooA.animateType(MarkerOptions.MarkerAnimateType.drop);
    mMarkerA = (Marker) (mBaiduMap.addOverlay(ooA));
}
//设置marker的点击监听
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            //显示marker对话框,按自己的需求定义
            showMarkerDialog(marker);
            return true;
        }
});

6.回到当前定位的位置视图

private double mCurrentLat = 0.0;
private double mCurrentLon = 0.0;
public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }                                   
            mCurrentLat = location.getLatitude();
            mCurrentLon = location.getLongitude();           
        }

        public void onReceivePoi(BDLocation poiLocation) {
        }
 }
//回到当前定位的位置视图
private void backCurrentPosition(){
    LatLng ll = new LatLng(mCurrentLat,mCurrentLon);
    MapStatus.Builder builder = new MapStatus.Builder();
    builder.target(ll).zoom(13.0f);
    mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
}

相关文章

网友评论

      本文标题:记录android百度地图的一些细节功能的实现。

      本文链接:https://www.haomeiwen.com/subject/omwxvftx.html