Android 高德地图自定义地图

现在很多调用高德地图的app,都使用了自定义地图,比较个性。现在进入主题。

高德给出的开发文档https://lbs.amap.com/api/android-sdk/guide/create-map/custom/,参照做下去,发现离线加载给出的方法都已过时。参照高德api,发现替换方法为:

aMap.setCustomMapStyle(customMapStyleOptions);

而CustomMapStyleOptions提供的方法有setStyleData(byte[] var1) 、setStyleExtraData(byte[] var1)。

将下载下来的配置文件xxx.data放入assets文件中,最终读取成byte[],给到上述set方法。就可以离线加载自定义地图了。

贴出方法如下:

byte[]  buffer1 = null;
byte[]  buffer2 = null;
InputStream is1 = null;
InputStream is2 = null;
try {
    is1 = _mActivity.getAssets().open("style.data");
    int lenght1 = is1.available();
    buffer1 = new byte[lenght1];
    is1.read(buffer1);
    is2 = _mActivity.getAssets().open("style_extra.data");
    int lenght2 = is2.available();
    buffer2 = new byte[lenght2];
    is2.read(buffer2);
} catch (IOException e) {
    e.printStackTrace();
}finally {
    try {
        if (is1!=null)
            is1.close();
        if (is2!=null)
            is2.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}CustomMapStyleOptions customMapStyleOptions = new CustomMapStyleOptions();
customMapStyleOptions.setStyleData(buffer1);
customMapStyleOptions.setStyleExtraData(buffer2);
aMap.setCustomMapStyle(customMapStyleOptions);

你可能感兴趣的:(Android 高德地图自定义地图)