【问题标题】:Flutter Google Maps. Displaying style. How can I get this working?颤振谷歌地图。展现风格。我怎样才能得到这个工作?
【发布时间】:2020-11-07 07:39:35
【问题描述】:

由于 Google maps API 更改为基于 Widget 以及 Flutter 的某种更改,我收到以下错误:

Error: The method 'setMapStyle' isn't defined for the class 'GoogleMapController'.

我正在尝试从 .json 或 .txt 为我的地图加载自定义样式。这是我的代码:

    SizedBox(
      width: MediaQuery.of(context)
          .size
          .width, // or use fixed size like 200
      height: 250,
      child: GoogleMap(
        onMapCreated: _onMapCreated,
        initialCameraPosition: CameraPosition(
          target: _center,
          zoom: 11.0,
        ),
      ),
    ),

关于创建和样式:

  void _onMapCreated(GoogleMapController controller) async {
    Completer<GoogleMapController> _controller = Completer();
    mapController = controller;
    _setStyle(controller);
  }

  void _setStyle(GoogleMapController controller) async {
    String value = await DefaultAssetBundle.of(context)
        .loadString('assets/map_style.text');
    controller.setMapStyle(value);
  }

有人知道如何解决这个问题吗? :/

【问题讨论】:

  • 如果您更新了包,功能可能已更改或已重命名

标签: google-maps flutter dart


【解决方案1】:

我用过这个方法,效果不错。

onMapCreated: (GoogleMapController controller) {
              controller.setMapStyle(Utils.mapStyles);}

我将 stylr 保存在 dart 文件中

  class Utils {
  static String mapStyles = '''[
  {
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#f5f5f5"
      }
    ]
  },
  {
    "elementType": "labels.icon",
    "stylers": [
      {
        "visibility": "off"
      }
    ]
  },
  {
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#616161"
      }
    ]
  },
  .......
]''';
}

【讨论】:

  • 这已编译但与地图没有明显不同。 o_o 会尝试插入我的 .json
  • 我的问题可能是我的孩子的所有东西都在一个尺寸不足的按钮中吗?编辑:我的意思是大小的盒子。
  • 我不这么认为...尝试将设置样式移动到第一个函数,而不是让两个函数更改控制器
  • 你是对的!我不得不删除宽度:MediaQuery.off(context).size.width.... WTF。真令人沮丧。我不敢相信这两天把我搞砸了。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2020-11-09
  • 1970-01-01
  • 2013-06-30
  • 2022-01-06
  • 1970-01-01
  • 2023-01-30
  • 2013-01-29
  • 1970-01-01
相关资源
最近更新 更多