【问题标题】:Dynamically create/download style for Google Maps为谷歌地图动态创建/下载样式
【发布时间】:2021-09-27 01:04:40
【问题描述】:

对于 Android 版 Google 地图,我有一个带有地图样式的 RAW 格式的 JSON 文件。

我是否可以在应用程序运行时动态下载 JSON 并使用它?

mMap = googleMap
mMap.setMapStyle(
    MapStyleOptions.loadRawResourceStyle(
        this, R.raw.mapstyle1
    )
)

我无法动态创建 RAW 文件

但我也无法替换 JSON 文件,因为设置只能通过 RAW 中的 resourceId 获取样式

有没有办法做到这一点?

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    使用公共构造函数MapStyleOptions(String json) 而不是结构loadRawResourceStyle(Context clientContext, int resourceId) 方法。类似的东西:

    ...
    StringBuilder mapStyleStringBuilder = new StringBuilder();
    
    // create you map style JSON
    mapStyleStringBuilder.append("[");
    mapStyleStringBuilder.append(" { "featureType": "all", "elementType": "geometry", "stylers": [ { "color": "#242f3e" } ] },");
    mapStyleStringBuilder.append("]");
    
    // convert it to String
    String dynamicallyCreatedStyleJson =  mapStyleStringBuilder.toString()
    
    // and apply to map
    mMap.setMapStyle(new MapStyleOptions(dynamicallyCreatedStyleJson));
    ...
    

    【讨论】:

      猜你喜欢
      • 2014-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 2019-04-21
      • 2018-04-12
      • 1970-01-01
      相关资源
      最近更新 更多