目录
2.2.1 放custom_config_land.json到sdcard
1 个性化地图简介
个性化地图通关改变元素和文字颜色、可见性,实现地图多样的展现,BaiduMap SDK提供了13大类、14个子类,包含陆地、
水系、绿地、建筑物、高速公路、城市公里、铁路、地图、景区标注、机场标注等等;
BaiduMap实现个性化需要json代码来设置 ,可以通过个性地图编辑工具生成地图样式json;
2 开发步骤
2.1 读取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 开启个性化地图
2.2.1 放custom_config_land.json到sdcard
[
{
"featureType": "land",
"elementType": "geometry",
"stylers": {
"color": "#800000ff",
"level": "12"
}
},
{
"featureType": "land",
"elementType": "geometry",
"stylers": {
"color": "#800080ff"
}
}
]
2.2.2 动态创建MapView
动态创建MapView添加到布局中,必须在创建前设置MapView.setCustomMapStylePath(个性化的json文件路径);
//个性化文件
String path = "/mnt/sdcard/custom_config_land.json"
//MapView必须创建之前必须设置MapView.setCustomMapStylePath
setMapCustomFile(context, path);
//创建MapView对象
mMapView = new MapView(context, new BaiduMapOptions());
//添加MapView到布局中
ll_mapView.addView(mMapView);
//开启个性化地图
MapView.setMapCustomEnable(true);
2.2.3 xml中添加MapView
在xml文件中添加:
<com.baidu.mapapi.map.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"></com.baidu.mapapi.map.MapView>
在setContentView()之前添加MapView.setCustomMapStylePath(个性化的json文件路径);
private String path = "/mnt/sdcard/custom_config_land.json";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//先设置MapView.setCustomMapStylePath(path)
setMapCustomFile(context, path);
//设置ContentView
setContentView(R.layout.activity_mapcustom);
//开启个性化地图
MapView.setMapCustomEnable(true);
}
2.3 关闭个性化地图
MapView.setMapCustomEnable(false);
2.4 多个个性化切换
多个个性化地图切换需要动态添加MapView到布局中,而且在切换之前要销毁地图控件和销毁地图,然后在重新添加;
//销毁地图控件
MapView.setMapCustomEnable(false);
//布局移除MapView
ll_mapView.removeView(mMapView);
//销毁MapView
mMapView.onDestroy();
//重新添加MapView
setMapCustomFile(context, path);
//创建MapView
mMapView = new MapView(context, new BaiduMapOptions());
//添加MapView到布局
ll_mapView.addView(mMapView);
//开启个性化地图
MapView.setMapCustomEnable(true);
2.5 onDestroy()销毁地图和控件
在onDestroy()中不能先销毁地图控件,再销毁地图,如果不销毁地图控件会造成崩溃;
@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();
}