百度地图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);
}
为啥打开后不能自己定位自己的位置:没有设置~_<自己百度吧