百度地图android自定义文字,androidsdk | 百度地图API SDK

方式一:配置.sty样式ID

// 地图View实例

private MapView mMapView;

// 用于设置个性化地图的样式文件

private static final String CUSTOM_FILE_NAME_CX = "custom_map_config_CX.sty";

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mMapView = new MapView(this, new BaiduMapOptions());

FrameLayout frameLayout = new FrameLayout(this);

frameLayout.addView(mMapView);

setContentView(frameLayout);

// 获取.sty文件路径

String customStyleFilePath = getCustomStyleFilePath(CustomMapDemo.this, CUSTOM_FILE_NAME_CX);

MapCustomStyleOptions mapCustomStyleOptions = new MapCustomStyleOptions();

mapCustomStyleOptions.localCustomStylePath(customStyleFilePath); //本地离线样式文件路径,如果在线方式加载失败,会默认加载本地样式文件。

mapCustomStyleOptions.customStyleId("d868bebd0fe24fa59381acbcd4db24e3"); //在线样式文件对应的id。

mMapView.setMapCustomStyle(mapCustomStyleOptions, new CustomMapStyleCallBack() {

@Override

public boolean onPreLoadLastCustomMapStyle(String customStylePath) {

return false; //默认返回false,由SDK内部处理加载逻辑;返回true则SDK内部不会做任何处理,由开发者自行完成样式加载。

}

@Override

public boolean onCustomMapStyleLoadSuccess(boolean hasUpdate, String customStylePath) {

return false; //默认返回false,由SDK内部处理加载逻辑;返回true则SDK内部不会做任何处理,由开发者自行完成样式加载。

}

@Override

public boolean onCustomMapStyleLoadFailed(int status, String Message, String customStylePath) {

return false; //默认返回false,由SDK内部处理加载逻辑;返回true则SDK内部不会做任何处理,由开发者自行完成样式加载。

}

});

}

@Override

protected void onResume() {

super.onResume();

// 在activity执行onResume时必须调用mMapView.onResume()

mMapView.onResume();

}

@Override

protected void onPause() {

super.onPause();

//在activity执行onPause时必须调用mMapView.onPause()

mMapView.onPause();

}

@Override

protected void onDestroy() {

super.onDestroy();

// 在activity执行onDestroy时必须调用mMapView.onDestroy()

mMapView.onDestroy();

}

方式二:加载样式文件

a、 在项目中添加自定义样式文件 如:将离线样式文件存放在assets目录下。

b0efa5c0171d016b9c3429d7b8035202.png

b、 传入样式文件路径:

V6.0.0版本起,建议使用新的个性化地图API,能够实现动态更改样式(同一地图设置不同的样式),同时适配多地图场景(不同地图设置不同的样式),并且样式文件路径设置API不再要求在地图创建之前调用,地图对象创建完成之后设置即可。在地图对象释放时,也无需关闭个性化开关。详细区别见个性化地图元素说明规则

// 地图View实例

private MapView mMapView;

// 用于设置个性化地图的样式文件

private static final String CUSTOM_FILE_NAME_CX = "custom_map_config_CX.sty";

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mMapView = new MapView(this, new BaiduMapOptions());

FrameLayout frameLayout = new FrameLayout(this);

frameLayout.addView(mMapView);

setContentView(frameLayout);

// 获取.sty文件路径

String customStyleFilePath = getCustomStyleFilePath(CustomMapDemo.this, CUSTOM_FILE_NAME_CX);

// 设置个性化地图样式文件的路径和加载方式

mMapView.setMapCustomStylePath(customStyleFilePath);

// 动态设置个性化地图样式是否生效

mMapView.setMapCustomStyleEnable(true);

}

@Override

protected void onResume() {

super.onResume();

// 在activity执行onResume时必须调用mMapView.onResume()

mMapView.onResume();

}

@Override

protected void onPause() {

super.onPause();

//在activity执行onPause时必须调用mMapView.onPause()

mMapView.onPause();

}

@Override

protected void onDestroy() {

super.onDestroy();

// 在activity执行onDestroy时必须调用mMapView.onDestroy()

mMapView.onDestroy();

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值