百度地图4.1_1开发教程(5)Overlay、Polyline

本章介绍画线条,覆盖物的方法

首先在布局中加入
<Switch
        android:id="@+id/activity_main_sc_addLay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        android:text="添加覆盖物"/>


    <Switch
        android:id="@+id/activity_main_sc_addLine"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/activity_main_sc_addLay"
        android:background="@color/colorPrimary"
        android:text="添加线条"/>
在类中,继承 implements CompoundButton.OnCheckedChangeListener
 @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        switch (buttonView.getId())
        {
            case R.id.activity_main_sc_addLay: // 覆盖物
                if (isChecked)
                {
                    if (myOverlay1 == null)
                    {
                        bdGround = BitmapDescriptorFactory
                                .fromResource(R.mipmap.ground_overlay);
                        // 添加覆盖物区域
                        LatLng southwest = new LatLng(39.92235, 116.380338);  //西南
                        LatLng northeast = new LatLng(39.947246, 116.414977);    // 东北
                        LatLngBounds bounds = new LatLngBounds.Builder().include(northeast)
                                .include(southwest).build();

                        GroundOverlayOptions ooGround = new GroundOverlayOptions()
                                .positionFromBounds(bounds).image(bdGround).transparency(0.8f);
                        myOverlay1 = mBaiduMap.addOverlay(ooGround);
                    }
                } else
                {
                    if (myOverlay1 != null)
                    {
                        myOverlay1.remove();
                        myOverlay1 = null;
                    }
                }
                break;

            case R.id.activity_main_sc_addLine: // 线条
                if (isChecked)
                {
                    if (mPolyline == null)
                    {
                        LatLng p1 = new LatLng(39.97923, 116.357428);
                        LatLng p2 = new LatLng(39.94923, 116.397428);
                        LatLng p3 = new LatLng(39.97923, 116.437428);
                        LatLng p4 = new LatLng(39.97923, 116.437206);

                        List<LatLng> points = new ArrayList<>();
                        points.add(p1);
                        points.add(p2);
                        points.add(p3);
                        points.add(p4);
                        OverlayOptions ooPolyline = new PolylineOptions().width(10)
                                .color(0xAAFF0000).points(points);
                        mPolyline = (Polyline) mBaiduMap.addOverlay(ooPolyline);
                    }
                } else
                {
                    if (mPolyline != null)
                    {
                        mPolyline.remove();
                        mPolyline = null;
                    }
                }
                break;
        }
    }
在方法中,如果按钮在选中状态下切覆盖物没有被创建,就开始创建
其中,bdGround是一个矩形,覆盖物是由两个点构成的,有些人可能会感觉奇怪,其实没什么好说的,左下角一个坐标,右上角一个坐标,这样就构成了一个矩形。这里我在代码注释中已经写的非常详细了。线条也是同样的道理。如果你有需求,要求地图上某一类覆盖物或线条消失,你可以参考我的百度地图教程3,换汤不换药,相信看了以后就会有思路了。
注:如果只是在项目中添加一个点、覆盖物等,大可不必按照我的写法来,完全参照官方文档就可以了,如果你项目中有很多点,覆盖物,线条,你可以尝试用我的写法,在稍作判断后用循环将线条或覆盖物remove掉。

Demo地址
http://download.csdn.net/detail/u012552275/9680962


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值