目录
1.2 陆地(custom_config_land.json)
1.3水系(custom_config_water.json)
1.4 绿地(custom_config_green.json)
1.5 行政边界(custom_config_district.json)
1.6 旅游景区(custom_config_scenic.json)
1.7 机场(custom_config_airport.json)
1. assets中添加多个json文件
1.1 json文件项目结构
1.2 陆地(custom_config_land.json)
[
{
"featureType": "land",
"elementType": "geometry",
"stylers": {
"color": "#800000ff",
"level": "12"
}
},
{
"featureType": "land",
"elementType": "geometry",
"stylers": {
"color": "#800080ff"
}
}
]
1.3水系(custom_config_water.json)
[
{
"featureType": "water",
"elementType": "geometry",
"stylers": {
"color": "#4169E1ff"
}
}
]
1.4 绿地(custom_config_green.json)
[
{
"featureType": "green",
"elementType": "geometry",
"stylers": {
"color": "#008000ff"
}
}
]
1.5 行政边界(custom_config_district.json)
[
{
"featureType": "districtlabel",
"elementType": "labels.text.stroke",
"stylers": {
"color": "#13171dff"
}
},
{
"featureType": "districtlabel",
"elementType": "labels.text.fill",
"stylers": {
"color": "#4e5961ff"
}
}
]
1.6 旅游景区(custom_config_scenic.json)
[
{
"featureType": "scenicspotslabel",
"elementType": "all",
"stylers": {
"color": "#800000ff"
}
},
{
"featureType": "scenicspots",
"elementType": "all",
"stylers": {
"color": "#800000ff"
}
}
]
1.7 机场(custom_config_airport.json)
[
{
"featureType": "airportlabel",
"elementType": "all",
"stylers": {
"color": "#800000ff"
}
},
{
"featureType": "airportlabel",
"elementType": "geometry.fill",
"stylers": {
"color": "#800000ff"
}
},
{
"featureType": "airportlabel",
"elementType": "labels.text.fill",
"stylers": {
"color": "#800000ff"
}
}
]
2 代码编写
2.1 xm代码
2.2 Activity代码
2.2.1 初始化所有view
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapcustom);
context = this;
//初始化View
initView();
//初始化数据和事件
initData();
}
//初始化View
private void initView() {
rb_custom = (RadioGroup) findViewById(R.id.rb_custom);
txt_coding = (TextView) findViewById(R.id.txt_coding);
rb_open_custom = (RadioButton) findViewById(R.id.rb_open_custom);
rb_close_custom = (RadioButton) findViewById(R.id.rb_close_custom);
spinner = (Spinner) findViewById(R.id.spinner);
ll_mapView = (LinearLayout) findViewById(R.id.ll_mapView);
mMapView = new MapView(context, new BaiduMapOptions());
ll_mapView.addView(mMapView);
}
2.2.2 初始化数据和事件
private void initData() {
List spinnerList = new ArrayList();
spinnerList.add("个性化");
spinnerList.add("陆地");
spinnerList.add("水系");
spinnerList.add("绿地");
spinnerList.add("行政区边界");
spinnerList.add("景区");
spinnerList.add("机场");
spinnerList.add("地铁");
spinnerList.add("教育和医疗");
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, spinnerList);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new SpinnerOnSelect());
rb_custom.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.rb_open_custom://显示个性地图
MapView.setMapCustomEnable(true);
spinner.setVisibility(View.VISIBLE);
break;
case R.id.rb_close_custom://关闭个性地图
MapView.setMapCustomEnable(false);
spinner.setVisibility(View.INVISIBLE);
break;
}
}
});
}
2.2.3 个性化切换事件
private class SpinnerOnSelect implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView> adapterView, View view, int i, long l) {
switch (i) {
case 0://全部
refreshMap("custom_config_dark.json");
break;
case 1://陆地
refreshMap("custom_config_land.json");
break;
case 2://水系
refreshMap("custom_config_water.json");
break;
case 3://绿地
refreshMap("custom_config_green.json");
break;
case 4://行政区边界
refreshMap("custom_config_district.json");
break;
case 5://景点
refreshMap("custom_config_scenic.json");
break;
case 6://机场
refreshMap("custom_config_airport.json");
break;
}
}
@Override
public void onNothingSelected(AdapterView> adapterView) {
}
}
2.2.4 刷新地图(设置个性化地图)
/**
* 设置个性化地图
*/
private void refreshMap(String path) {
MapView.setMapCustomEnable(false);
ll_mapView.removeView(mMapView);
mMapView.onDestroy();
setMapCustomFile(context, path);
mMapView = new MapView(context, new BaiduMapOptions());
ll_mapView.addView(mMapView);
MapView.setMapCustomEnable(true);
}
2.2.5 加载assets中的json
// 设置个性化地图config文件路径
private void setMapCustomFile(Context context, String PATH) {
FileOutputStream out = null;
InputStream inputStream = null;
String moduleName = null;
try {
inputStream = context.getAssets()
.open("customConfigdir/" + PATH);
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
moduleName = context.getFilesDir().getAbsolutePath();
File f = new File(moduleName + "/" + PATH);
if (f.exists()) {
f.delete();
}
f.createNewFile();
out = new FileOutputStream(f);
out.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
MapView.setCustomMapStylePath(moduleName + "/" + PATH);
}
2.2.6 onDestroy()销毁地图组件和地图
onDestory()如果直销毁MapView,不销毁地图组件或造成崩溃;
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
MapView.setMapCustomEnable(false);
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}