BaiduMap SDK-GroundOverlay案例

1. 添加GroundOverlay两种方式

1.1 positionFromBounds(LatLngBounds bounds)

        LatLng southwest = new LatLng(39.92235, 116.380338);
        LatLng northeast = new LatLng(39.947246, 116.414977);

        LatLngBounds bounds = new LatLngBounds.Builder().include(southwest).include(northeast).build();

        GroundOverlayOptions options = new GroundOverlayOptions();
        //ground位置信息,通过西南与东北坐标范围
        options.positionFromBounds(bounds);
        //设置图片
        options.image(bitmapDescriptor);
        //ground透明度
        options.transparency(0.8f);
        //额外信息
        Bundle bundle = new Bundle();
        bundle.putString("redInfo","这个是测试使用的!");
        options.extraInfo(bundle);

        bludGroundOverlay = (GroundOverlay) baiduMap.addOverlay(options);

1.2 position(LatLng latLng)  

        LatLng southwest = new LatLng(39.91235, 116.390338);
   
        GroundOverlayOptions options = new GroundOverlayOptions();
        //ground位置信息,通过西南与东北坐标范围
        options.dimensions(4400,4400);
        //设置锚点
        options.anchor(0.8f,0.8f);
        options.position(southwest);
        //设置图片
        options.image(redDescriptor);
        //ground透明度
        options.transparency(0.8f);

        redGroundOverlay = (GroundOverlay) baiduMap.addOverlay(options);

2 案例完整代码

2.1 xml布局

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

    <com.baidu.mapapi.map.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></com.baidu.mapapi.map.MapView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/cb_blue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="隐藏蓝色"
            />

        <Button
            android:id="@+id/btn_zIndex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="层次" />
        <Button
            android:id="@+id/btn_extraInfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="额外信息"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_WH"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="宽高"/>
        <Button android:id="@+id/btn_anchor"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/cb_blue"
            android:text="Ground锚点比例"/>
    </LinearLayout>
        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:min="0"
            android:progress="80"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"/>
    </LinearLayout>
</RelativeLayout>

2.2 Activity代码

/**
 * 叠加覆盖物
 */
public class GroundOverlayActivity extends AppCompatActivity {
    private MapView mapView;
    private BaiduMap baiduMap;
    BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.ground_overlay);
    BitmapDescriptor redDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.ground_red_overlay);

    private GroundOverlay redGroundOverlay,bludGroundOverlay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ground_overlay);
        mapView = (MapView) findViewById(R.id.mapView);
        baiduMap = mapView.getMap();

        //设置地图级别
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.zoomTo(14);
        baiduMap.setMapStatus(mapStatusUpdate);

        initGroundOverlay();
        initRedGroundOverlay();
        //设置层次
        ((Button)findViewById(R.id.btn_zIndex)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bludGroundOverlay.setZIndex(2);
                redGroundOverlay.setZIndex(1);
            }
        });
        //重新设置覆盖物大小
        ((Button)findViewById(R.id.btn_WH)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                redGroundOverlay.setDimensions(5000,5000);
            }
        });
        //设置覆盖物锚点比例
        ((Button)findViewById(R.id.btn_anchor)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                redGroundOverlay.setAnchor(0.6f,0.5f);
            }
        });
        //获取额外信息
        ((Button)findViewById(R.id.btn_extraInfo)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String  extraStr = bludGroundOverlay.getExtraInfo().getString("redInfo");
                Toast.makeText(getApplicationContext(),extraStr,Toast.LENGTH_SHORT).show();
            }
        });
        //设置是否隐藏
        ((CheckBox)findViewById(R.id.cb_blue)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                bludGroundOverlay.setVisible(!b);
            }
        });
        ((SeekBar)findViewById(R.id.seekBar)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
                redGroundOverlay.setTransparency(seekBar.getProgress()/100.00f);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void initGroundOverlay() {
        LatLng southwest = new LatLng(39.92235, 116.380338);
        LatLng northeast = new LatLng(39.947246, 116.414977);

        LatLngBounds bounds = new LatLngBounds.Builder().include(southwest).include(northeast).build();

        GroundOverlayOptions options = new GroundOverlayOptions();
        //ground位置信息,通过西南与东北坐标范围
        options.positionFromBounds(bounds);
        //设置图片
        options.image(bitmapDescriptor);
        //ground透明度
        options.transparency(0.8f);
        //额外信息
        Bundle bundle = new Bundle();
        bundle.putString("redInfo","这个是测试使用的!");
        options.extraInfo(bundle);

        bludGroundOverlay = (GroundOverlay) baiduMap.addOverlay(options);
    }

    private void initRedGroundOverlay() {
        LatLng southwest = new LatLng(39.91235, 116.390338);

        GroundOverlayOptions options = new GroundOverlayOptions();
        //ground位置信息,通过西南与东北坐标范围
        options.dimensions(4400,4400);
        //设置锚点
        options.anchor(0.8f,0.8f);
        options.position(southwest);
        //设置图片
        options.image(redDescriptor);
        //ground透明度
        options.transparency(0.8f);

        redGroundOverlay = (GroundOverlay) baiduMap.addOverlay(options);
    }

    @Override
    protected void onResume() {
        mapView.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        mapView.onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mapView.onDestroy();
        if(bitmapDescriptor!=null){
            bitmapDescriptor.recycle();
        }
        super.onDestroy();
    }
}

猜你喜欢

转载自blog.csdn.net/niuba123456/article/details/81137849
今日推荐