BaiduMap SDK-多地图展示

目录

1.布局xml文件

2. 代码编写

2.1 定义4个LatLng变量

2.2 获取所有View

2.3 设置地图中心点和事件

2.3.1 设置地图中心点和事件

2.3.2 设置地图旋转角度(显示指南针)

2.3.3 地图事件

2.4 执行结果对比


一个界面展示可以多个地图,每个地图可以展示不同的信息,更具有个性化,满足对地图不同需求;

就是先在布局上添加几个父容器,每个容器设置class="com.baidu.mapapi.map.SupportMapFragment",

表示是一个MapView;

1.布局xml文件

定义4个MapView容器;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/black">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_weight="1"   
       android:background="@android:color/black"
        android:orientation="horizontal" >

        <fragment
            android:id="@+id/map1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            class="com.baidu.mapapi.map.SupportMapFragment" />

        <fragment
            android:id="@+id/map2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            class="com.baidu.mapapi.map.SupportMapFragment" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_weight="1"
        android:orientation="horizontal" >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
        <fragment
            android:id="@+id/map3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            class="com.baidu.mapapi.map.SupportMapFragment" >
        </fragment>
            <CheckBox android:id="@+id/cb_compass"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="显示指南针"
                android:checked="true"
                android:layout_alignParentRight="true"/>
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
        <fragment
            android:id="@+id/map4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            class="com.baidu.mapapi.map.SupportMapFragment" />
                <CheckBox android:id="@+id/cb_label"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="显示标注" android:checked="true"
                    android:layout_alignParentRight="true"/>
        </RelativeLayout>
    </LinearLayout>
</LinearLayout>

2. 代码编写

2.1 定义4个LatLng变量

4个LatLng变量主要用来设置每个地图的中心点;

   private static final LatLng GEO_BEIJING = new LatLng(39.945, 116.404);
    private static final LatLng GEO_SHANGHAI = new LatLng(31.227, 121.481);
    private static final LatLng GEO_GUANGZHOU = new LatLng(23.155, 113.264);
    private static final LatLng GEO_SHENGZHENG = new LatLng(22.560, 114.064);

2.2 获取所有View

SupportMapFragment mapView1, mapView2, mapView3, mapView4;

    private  CheckBox cb_compass,cb_label;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_map_view);

        mapView1 = (SupportMapFragment) (getSupportFragmentManager()
                .findFragmentById(R.id.map1));
        mapView2 = (SupportMapFragment) (getSupportFragmentManager()
                .findFragmentById(R.id.map2));
        mapView3 = (SupportMapFragment) (getSupportFragmentManager()
                .findFragmentById(R.id.map3));
        mapView4 = (SupportMapFragment) (getSupportFragmentManager()
                .findFragmentById(R.id.map4));
        cb_compass = (CheckBox) findViewById(R.id.cb_compass);
        cb_label = (CheckBox) findViewById(R.id.cb_label);
        initMap();
    }

2.3 设置地图中心点和事件

2.3.1 设置地图中心点和事件

/**
     * 初始化Map,设置不同城市为地图中心点,设置Logo不同位置
     */
    private void initMap() {
        /*北京为地图中心,logo在左上角*/
        MapStatusUpdate u1 = MapStatusUpdateFactory.newLatLng(GEO_BEIJING);
        mapView1.getBaiduMap().setMapStatus(u1);
        mapView1.getMapView().setLogoPosition(LogoPosition.logoPostionleftTop);

        /*上海为地图中心,logo在右上角*/
        MapStatusUpdate u2 = MapStatusUpdateFactory.newLatLng(GEO_SHANGHAI);
        mapView2.getBaiduMap().setMapStatus(u2);
        mapView2.getMapView().setLogoPosition(LogoPosition.logoPostionRightTop);

        /*广州为地图中心,logo在左下角*/
        MapStatusUpdate u3 = MapStatusUpdateFactory.newLatLng(GEO_GUANGZHOU);
        mapView3.getBaiduMap().setMapStatus(u3);
        mapView3.getMapView().setLogoPosition(LogoPosition.logoPostionCenterBottom);
        //显示左上角指南针
        initMpaStatus(mapView3.getBaiduMap());
        mapView3.getBaiduMap().getUiSettings().setCompassEnabled(true);
        //是否显示指南针
        cb_compass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                mapView3.getMapView().getMap().getUiSettings().setCompassEnabled(b);
            }
        });

        /*深圳为地图中心,logo在右下角*/
        MapStatusUpdate u4 = MapStatusUpdateFactory.newLatLng(GEO_SHENGZHENG);
        mapView4.getBaiduMap().setMapStatus(u4);
        mapView4.getMapView().setLogoPosition(LogoPosition.logoPostionRightBottom);
        //是否显示标注
        cb_label.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                mapView4.getMapView().getMap().showMapPoi(b);
            }
        });
    }

2.3.2 设置地图旋转角度(显示指南针)

地图左上角的指南针图标,只有地图进行旋转才会出现,为了在地图显示就出现指南针图标,可以通过代码旋转一定角度

就可以了;

  private void initMpaStatus(BaiduMap baiduMap) {
        float rotate = -12.0f;
        MapStatus mapStatus = new MapStatus.Builder(baiduMap.getMapStatus()).rotate(rotate).build();
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
        baiduMap.setMapStatus(mapStatusUpdate);
    }

2.3.3 地图事件

为了体现各个地图的不同,用来处理不同场景,特意第3个地图添加是否显示指南针,第4个地图添加是否显示标注;

cb_compass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
               //是否显示左上角的指南针               
               mapView3.getMapView().getMap().getUiSettings().setCompassEnabled(b);
            }
        });
  
cb_label.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
               //是否显示地图标注
                mapView4.getMapView().getMap().showMapPoi(b);
            }
        });

2.4 执行结果对比

执行显示罗盘和标注                                              执行不显示罗盘和标注

          

猜你喜欢

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