百度地图SDK地图切换_2022.06.22

百度地图SDK地图切换

经过前一个博客你已经学会了引用SDK到自己的项目里

下面看看怎么切换地图吧

布局文件


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

		<RadioGroup
			android:id="@+id/rg_TYPE"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:checkedButton="@id/rg_normally"
			android:orientation="horizontal">


			<RadioButton
				android:text="普通地图"
				android:id="@+id/rg_normally"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content" />

			<RadioButton
				android:text="卫星地图"
				android:id="@+id/rg_GPS"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content" />
		</RadioGroup>
		<CheckBox
			android:checked="false"
			android:text="路况图"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:id="@+id/rg_lk"/>

		<CheckBox
			android:checked="false"
			android:text="城市热力图"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:id="@+id/rg_rl"/>


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

java文件

使用到的文档https://lbsyun.baidu.com/index.php?title=androidsdk/guide/create-map/maptype


public class MainActivity extends AppCompatActivity {

    private int times = 0;
    private final int REQUEST_PERMISSION = 0;

    //地图
    private MapView mMapView = null;

    //布局
    private RadioGroup radioGroup;
    RadioButton rg_normally, rg_GPS;
    CheckBox rg_lk, rg_rl;
    private BaiduMap mBaiduMap;

    Boolean isFirstLoc = true;
    private MyLocationConfiguration.LocationMode locationMode;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();


        radioGroup = findViewById(R.id.rg_TYPE);
        rg_normally = findViewById(R.id.rg_normally);
        rg_GPS = findViewById(R.id.rg_GPS);
        rg_lk = findViewById(R.id.rg_lk);
        rg_rl = findViewById(R.id.rg_rl);



        initEvent();



    }


    private void initEvent() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == rg_normally.getId()) {
                    mBaiduMap.setMapType(MAP_TYPE_NORMAL);
                } else if (i == rg_GPS.getId()) {
                    mBaiduMap.setMapType(MAP_TYPE_SATELLITE);
                }
            }
        });


        rg_lk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //开启交通图
                mBaiduMap.setTrafficEnabled(b);
            }
        });

        rg_rl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                //开启热力图
                mBaiduMap.setBaiduHeatMapEnabled(b);
            }
        });

    }

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

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

    @Override
    protected void onDestroy() {
        mMapView.onDestroy();
        mMapView = null;
        super.onDestroy();
    }

    @Override
    protected void onStart() {  //启动地图定位
        mBaiduMap.setMyLocationEnabled(true);
        super.onStart();

    }


    @Override
    protected void onStop() {  //停止地图定位
        super.onStop();
        mBaiduMap.setMyLocationEnabled(false);
    }

   

为啥打开后不能自己定位自己的位置:没有设置~_<自己百度吧

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值