百度地图----地图覆盖物

百度地图—-覆盖物

ONE Goal ,ONE Passion !

百度地图SDK为广大开发者提供的基础地图和上面的各种覆盖物元素,具有一定的层级压盖关系,具体如下(从下至上的顺序):
1、基础底图(包括底图、底图道路、卫星图等);
2、瓦片图层(TileOverlay);
3、地形图图层(GroundOverlay);
4、热力图图层(HeatMap);
5、实时路况图图层(BaiduMap.setTrafficEnabled(true););
6、百度城市热力图(BaiduMap.setBaiduHeatMapEnabled(true););
7、底图标注(指的是底图上面自带的那些POI元素);
8、几何图形图层(点、折线、弧线、圆、多边形);
9、标注图层(Marker),文字绘制图层(Text);
10、指南针图层(当地图发生旋转和视角变化时,默认出现在左上角的指南针);
11、定位图层(BaiduMap.setMyLocationEnabled(true););
12、弹出窗图层(InfoWindow);
13、自定义View(MapView.addView(View););


下面介绍几个常用的地图覆盖物

自动定位


  private BDLocation loc;//记录初次定位的位置
 // 开启定位图层
                mBaiduMap.setMyLocationEnabled(true);

 // 定位初始化
                mLocClient = new LocationClient(getApplication());

                LocationClientOption option = new LocationClientOption();
                option.setOpenGps(true);// 打开gps
                option.setCoorType("bd09ll"); // 设置坐标类型
                mLocClient.setLocOption(option);
                mLocClient.start();
                mLocClient.registerLocationListener(new MyLocationListenner());


/**
     * 自动定位SDK监听函数
     */

    public class MyLocationListenner implements BDLocationListener {

        private LatLng ll;

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置


            if (location == null || mMapView == null)
                return;
            loc = location;
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                            // 此处设置开发者获取到的方向信息,顺时针0-360
                    .direction(100).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            if (isFirstLoc) {
                isFirstLoc = false;
                ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
                //让地图执行位置移动动画
                mBaiduMap.animateMapStatus(u);

            }


            //发起反地理编码请求----下面有解释
            ReverseGeoCodeOption options = new ReverseGeoCodeOption().location(ll);
            geocode.reverseGeoCode(options);


        }


    }

关于地理地址编码与反编码
http://blog.csdn.net/fengltxx/article/details/50552575


添加弹出窗覆盖物

 /**
     * 添加弹出窗覆盖物
     *
     * @param address 为address添加覆盖物
     * @param ll      //添加的坐标
     */
    public void addInfoWindow(String address, LatLng ll) {

        //创建InfoWindow展示的view-----弹出窗覆盖物
        final TextView button = new Button(getApplicationContext());
        button.setBackgroundResource(R.drawable.pop);
        button.setTextColor(Color.BLACK);
        button.setText(address);
        //创建InfoWindow , 传入 view, 地理坐标, y 轴偏移量
        InfoWindow mInfoWindow = new InfoWindow(button, ll, -20);
        //显示InfoWindow
        mBaiduMap.showInfoWindow(mInfoWindow);

    }

添加标注

/**
     * 添加标注的方法
     *
     * @param point
     */
    public void addMarker(LatLng point) {

        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.icon_marka);
        //构建MarkerOption,用于在地图上添加Marker
        OverlayOptions option_mark = new MarkerOptions()
                .position(point)
                .icon(bitmap);
        //在地图上添加Marker,并显示
        Overlay marker = mBaiduMap.addOverlay(option_mark);


    }

下面是以上几种方法使用的合集,写的比较乱.嘿嘿…代码不是最重要的,重要的是如何去使用百度提供给我们的接口


public class MainActivity extends Activity {

    TextView tv;
    Button btn_myLoc;//定位
    Button btn_mark; //markar覆盖物


    MapView mMapView = null;
    private BaiduMap mBaiduMap;
    private GeoCoder geocode;
    private String address = "";

    /**
     * 定位相关
     */
    LocationClient mLocClient;
    boolean isFirstLoc = true;// 是否首次定位
    private BDLocation loc;//记录初次定位的位置
    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            tv.setText(msg.obj + "");

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //在使用SDK各组件之前初始化context信息,传入ApplicationContext
        //注意该方法要再setContentView方法之前实现
        SDKInitializer.initialize(getApplicationContext());

        setContentView(R.layout.activity_main);

        mMapView = (MapView) findViewById(R.id.bmapView);


        tv = (TextView) findViewById(R.id.tv);
        btn_myLoc = (Button) findViewById(R.id.btn_myLoc);
        btn_mark = (Button) findViewById(R.id.btn_mark);

        mBaiduMap = mMapView.getMap();

        //隐藏百度地图自带的按钮
        mMapView.showZoomControls(false);


        /**
         *   自动定位
         */
        btn_myLoc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                System.out.println("点击了定位");
                // 开启定位图层
                mBaiduMap.setMyLocationEnabled(true);

                // 定位初始化
                mLocClient = new LocationClient(getApplication());

                LocationClientOption option = new LocationClientOption();
                option.setOpenGps(true);// 打开gps
                option.setCoorType("bd09ll"); // 设置坐标类型
                mLocClient.setLocOption(option);
                mLocClient.start();
                mLocClient.registerLocationListener(new MyLocationListenner());


            }
        });

        /**
         *    添加 markar覆盖物
         *    34.7568711
         ,    113.663221
         */
        btn_mark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //定义Maker坐标点
                LatLng point = new LatLng(34.7568711
                        , 113.663221);
//构建Marker图标
                BitmapDescriptor bitmap = BitmapDescriptorFactory
                        .fromResource(R.drawable.icon_marka);
//构建MarkerOption,用于在地图上添加Marker
                OverlayOptions option_mark = new MarkerOptions()
                        .position(point)
                        .icon(bitmap);
//在地图上添加Marker,并显示
                Overlay marker = mBaiduMap.addOverlay(option_mark);

            }
        });


        /**
         *    地图点击监听
         */
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {

            /**
             *   点击地图上没有自动标注的地方触发
             *   在此处理点击事件
             * @param point
             */
            public void onMapClick(LatLng point) {



                System.out.println("point-----" + point);

            }

            /**
             * 在此处理底图标注点击事件
             * @param poi
             * @return
             */
            public boolean onMapPoiClick(MapPoi poi) {


                System.out.println("poi-------" + poi);
                return false;
            }
        });


        /**
         *     mark点击物的监听
         */
        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {


                System.out.println("-----markar被点击" + marker);
                return false;
            }
        });

        /**
         *      地图中心点的位置改变监听
         */
        mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {
            @Override
            public void onMapStatusChangeStart(MapStatus mapStatus) {

            }

            @Override
            public void onMapStatusChange(MapStatus mapStatus) {

            }

            @Override
            public void onMapStatusChangeFinish(MapStatus mapStatus) {

                //获得中心点坐标
                LatLng lat = mapStatus.target;
                getButtonAddress(lat);

                //发起反地理编码请求
                ReverseGeoCodeOption options = new ReverseGeoCodeOption().location(lat);
                geocode.reverseGeoCode(options);
            }
        });


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
        mMapView.onPause();
    }


    /**
     *  通过反地址编码获得地址信息
     * @param ll   中心点坐标
     */
    public void getButtonAddress(final LatLng ll) {
        //通过反地址编码获得地址信息
        geocode = GeoCoder.newInstance();
        //设置查询结果监听者
        geocode.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {

            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
                if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                    return;
                }
                if (result != null && result.error == SearchResult.ERRORNO.NO_ERROR) {

                    //得到位置
                    address = result.getAddress();



                    //创建InfoWindow展示的view
                    final TextView button = new Button(getApplicationContext());
                    button.setBackgroundResource(R.drawable.pop);
                    button.setTextColor(Color.BLACK);
                    button.setText(address);
                    //创建InfoWindow , 传入 view, 地理坐标, y 轴偏移量
                    InfoWindow mInfoWindow = new InfoWindow(button, ll, -20);
                    //显示InfoWindow
                    mBaiduMap.showInfoWindow(mInfoWindow);


                    //显示地址   ---- 通过handler显示位置
                    Message msg = Message.obtain();
                    msg.obj = address;
                    handler.sendMessage(msg);


                }
            }

            @Override
            public void onGetGeoCodeResult(GeoCodeResult arg0) {
            }
        });
    }


    /**
     * 自动定位SDK监听函数
     */

    public class MyLocationListenner implements BDLocationListener {

        private LatLng ll;

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置


            if (location == null || mMapView == null)
                return;
            loc = location;
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                            // 此处设置开发者获取到的方向信息,顺时针0-360
                    .direction(100).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            if (isFirstLoc) {
                isFirstLoc = false;
                ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
                mBaiduMap.animateMapStatus(u);

            }

            getButtonAddress(ll);
            //发起反地理编码请求
            ReverseGeoCodeOption options = new ReverseGeoCodeOption().location(ll);


        }

        public void onReceivePoi(BDLocation poiLocation) {
        }
    }

}

简单的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />

        <Button
            android:id="@+id/btn_myLoc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="定位" />

        <Button
            android:id="@+id/btn_mark"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示marker" />


    </LinearLayout>


    <com.baidu.mapapi.map.MapView
        android:id="@+id/bmapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true" />


</LinearLayout>
后续的地图功能慢慢完善
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值